Custom Directives
Learn how to create reusable Angular directives for custom behavior and interaction with HTML elements.
What are Custom Directives?
Custom directives are developer-created Angular directives used to add reusable behavior to HTML elements.
They are useful when the same DOM behavior or UI interaction needs to be used in multiple places in an application.
Custom Directive Flow
Create
Create a directive using the Angular CLI.
Define
Define the required directive behavior.
Apply
Apply the directive to an HTML element.
Reuse
Reuse the behavior wherever it is required.
Why Create Custom Directives?
Reusable
The same behavior can be reused on multiple elements.
Custom Behavior
Implement behavior specific to your application.
Maintainable
Keep common DOM behavior in one place.
UI Enhancement
Improve the interaction and appearance of UI elements.
Creating a Custom Directive
Angular CLI can generate a directive automatically.
ng generate directive highlight
Short form:
ng g directive highlight
Generated Directive
import { Directive } from '@angular/core';
@Directive({
selector: '[appHighlight]'
})
export class HighlightDirective {
}
@Directive decorator tells Angular that
this class represents a directive.
Directive Selector
The selector defines how the custom directive is applied in the HTML template.
@Directive({
selector: '[appHighlight]'
})
<p appHighlight>
Angular Training
</p>
appHighlight attribute.
Using ElementRef
ElementRef provides access to the native element
on which the directive is applied.
import { Directive, ElementRef } from '@angular/core';
@Directive({
selector: '[appHighlight]'
})
export class HighlightDirective {
constructor(private element: ElementRef) {
this.element.nativeElement.style.backgroundColor = 'yellow';
}
}
appHighlight receives
a yellow background.
Using HostListener
HostListener allows a directive to respond to
events generated by its host element.
import {
Directive,
ElementRef,
HostListener
} from '@angular/core';
@Directive({
selector: '[appHighlight]'
})
export class HighlightDirective {
constructor(private element: ElementRef) {
}
@HostListener('mouseenter')
onMouseEnter() {
this.element.nativeElement.style.backgroundColor = 'yellow';
}
@HostListener('mouseleave')
onMouseLeave() {
this.element.nativeElement.style.backgroundColor = '';
}
}
<p appHighlight>
Move the mouse over this text
</p>
Passing Data to a Directive
A custom directive can receive values from the component using an input property.
import { Directive, Input, ElementRef } from '@angular/core';
@Directive({
selector: '[appHighlight]'
})
export class HighlightDirective {
@Input() appHighlight = '';
constructor(private element: ElementRef) {
}
}
<p [appHighlight]="'lightblue'">
Angular Training
</p>
Complete Highlight Directive Example
import {
Directive,
ElementRef,
HostListener
} from '@angular/core';
@Directive({
selector: '[appHighlight]'
})
export class HighlightDirective {
constructor(private element: ElementRef) {
}
@HostListener('mouseenter')
onMouseEnter() {
this.element.nativeElement.style.backgroundColor = 'lightblue';
}
@HostListener('mouseleave')
onMouseLeave() {
this.element.nativeElement.style.backgroundColor = '';
}
}
<h3 appHighlight>
Pradnya
</h3>
<p appHighlight>
Angular Full Stack Student
</p>
Pradnya
Angular Full Stack Student
Move the mouse over an element to see the directive behavior.Common Use Cases
Highlighting
Highlight elements on hover or based on conditions.
Focus
Automatically focus an input element.
Animation
Add reusable interaction or animation behavior.
Validation
Add reusable UI behavior around validation.
Built-in vs Custom Directives
| Type | Purpose | Example |
|---|---|---|
| Built-in | Provided by Angular | ngClass |
| Built-in | Controls DOM structure | *ngIf |
| Custom | Application-specific behavior | appHighlight |
Best Practices
- Give custom directives clear and meaningful names.
- Keep a directive focused on one reusable behavior.
- Use
HostListenerfor host element events. - Use inputs when the directive needs configurable values.
- Avoid putting large business logic inside directives.
- Reuse directives when the same UI behavior appears in multiple places.
Summary
- Custom directives provide reusable application-specific behavior.
- Use the Angular CLI to generate directives quickly.
@Directivedefines a custom directive.ElementRefprovides access to the host element.HostListenerallows the directive to respond to events.- Inputs can be used to pass values into a directive.
- Custom directives help keep UI behavior reusable and maintainable.