Hey everyone! 👋
I’ve been diving into Angular recently, and one of the most fascinating concepts I’ve come across is directives. If you’re new to Angular like me, you might have heard the term but aren’t entirely sure what it means or how to use it. So, I decided to break it down and share what I’ve learned!
1. What Are Directives in Angular?
In simple terms, directives are instructions in the DOM (Document Object Model) that tell Angular how to manipulate elements. They’re a powerful feature that allows you to extend HTML with custom behavior. Angular provides three main types of directives:
- Component Directives: These are the most common type. Every Angular component is essentially a directive with a template.
- Attribute Directives: These change the appearance or behavior of an element. For example,Â
ngStyle
 andÂngClass
 are built-in attribute directives. - Structural Directives: These change the DOM layout by adding or removing elements. Examples includeÂ
*ngIf
 andÂ*ngFor
.
2. How to Use Directives
Let’s look at some examples to understand how directives work:
1. Structural Directives
Structural directives are prefixed with an asterisk (*
). They’re used to conditionally render or repeat elements.
<div *ngIf="isLoggedIn">
Welcome back, user!
</div>
<ul>
<li *ngFor="let item of items">{{ item }}</li>
</ul>
In the above example:
*ngIf
 conditionally displays theÂdiv
 ifÂisLoggedIn
 is true.*ngFor
 loops through theÂitems
 array and creates a list item for each element.
2. Attribute Directives
Attribute directives are used to change the appearance or behavior of an element.
<p [ngStyle]="{ 'color': textColor, 'font-size': fontSize }">
This text is styled dynamically!
</p>
<div [ngClass]="{ 'active': isActive, 'disabled': isDisabled }">
This div has dynamic classes.
</div>
Here:
ngStyle
 applies inline styles dynamically.ngClass
 adds or removes CSS classes based on conditions.
3. Custom Directives
You can also create your own directives to add custom behavior. For example, let’s create a directive that highlights an element on hover:
import { Directive, ElementRef, HostListener } from '@angular/core';
@Directive({
selector: '[appHighlight]'
})
export class HighlightDirective {
constructor(private el: ElementRef) {}
@HostListener('mouseenter') onMouseEnter() {
this.highlight('yellow');
}
@HostListener('mouseleave') onMouseLeave() {
this.highlight(null);
}
private highlight(color: string | null) {
this.el.nativeElement.style.backgroundColor = color;
}
}
To use this directive, simply add it to an element:
<p appHighlight>Hover over me to see the effect!</p>
3. Why Are Directives Important?
Directives are a core part of Angular because they allow you to create dynamic, reusable, and maintainable code. Whether you’re using built-in directives or creating custom ones, they help you build interactive and efficient applications.
4. Final Thoughts
Directives might seem a bit overwhelming at first, but once you start using them, they become an essential tool in your Angular toolkit. I’m still learning, but I hope this post helps you get started with directives too!
If you have any tips or resources to share, feel free to drop them in the comments. Let’s learn together!
Happy coding! 🚀