Attribute Directives
Learn how Angular attribute directives change the appearance or behavior of existing HTML elements.
What are Attribute Directives?
Attribute directives are Angular directives that modify the behavior, appearance, or properties of an existing HTML element.
Unlike structural directives, attribute directives do not create or remove elements from the DOM.
Attribute Directive Flow
HTML Element
Create an element in the Angular template.
Directive
Apply an attribute directive to the element.
Angular
Angular evaluates the directive and its values.
UI Update
The appearance or behavior changes.
Common Attribute Directives
ngClass
Dynamically adds or removes CSS classes.
ngStyle
Dynamically applies CSS styles.
Attribute Binding
Dynamically changes HTML attributes.
Custom Directive
Adds reusable custom behavior to elements.
ngClass Directive
The ngClass directive allows CSS classes to be
added or removed dynamically.
isActive = true;
isPremium = false;
<div [ngClass]="{
active: isActive,
premium: isPremium
}">
Student Profile
</div>
active class is applied because
isActive is true.
ngClass with Multiple Classes
Multiple CSS classes can be controlled using an object.
<div [ngClass]="{
active: isActive,
premium: isPremium,
highlighted: isHighlighted
}">
Student Details
</div>
ngStyle Directive
The ngStyle directive dynamically applies one or
more CSS styles to an element.
textColor = 'purple';
fontSize = 20;
<p [ngStyle]="{
color: textColor,
'font-size.px': fontSize
}">
Angular Full Stack Training
</p>
Attribute Binding
Angular can dynamically change HTML attributes using attribute binding.
<button
[attr.disabled]="isDisabled ? true : null">
Submit
</button>
disabled attribute is added when
isDisabled is true.
ngClass vs ngStyle
| Directive | Purpose | Example |
|---|---|---|
ngClass |
Apply CSS classes dynamically | [ngClass]="classes" |
ngStyle |
Apply CSS styles dynamically | [ngStyle]="styles" |
attr |
Change HTML attributes | [attr.title]="title" |
Complete Student Example
export class StudentComponent {
studentName = 'Pradnya';
isActive = true;
isPremium = true;
textColor = 'purple';
fontSize = 20;
}
<div
[ngClass]="{
active: isActive,
premium: isPremium
}"
[ngStyle]="{
color: textColor,
'font-size.px': fontSize
}">
<h3>{{studentName}}</h3>
<p>Angular Full Stack Student</p>
</div>
Pradnya
Angular Full Stack Student
Benefits of Attribute Directives
Reusable
Directive behavior can be reused across multiple elements.
Dynamic
UI appearance can change according to component data.
UI Control
Control classes, styles, and attributes dynamically.
Customizable
Developers can create their own attribute directives.
Best Practices
- Use
ngClassfor dynamic CSS classes. - Use
ngStylefor dynamic style values. - Prefer class-based styling for reusable UI designs.
- Keep directive expressions simple and readable.
- Use meaningful class and directive names.
- Create custom directives only when reusable behavior is required.
Summary
- Attribute directives modify existing HTML elements.
ngClassdynamically manages CSS classes.ngStyledynamically manages CSS styles.- Attribute binding can dynamically control HTML attributes.
- Attribute directives do not change the DOM structure.
- Custom directives can provide reusable application behavior.