Explain it simply
Custom Directives, in plain language
Custom Directive s let you create your own logic to modify the behavior of DOM elements.
Problem → Why → Solution
The problem — why this concept matters
Unrestricted direct use of document or nativeElement can break SSR, testing, and security boundaries.
A common mistake
One happy-path result does not prove the feature works; empty, loading, error, and cleanup behavior need explicit tests.
Core concept — understand how it works
An attribute directive adds reusable behavior to an existing element or component. It can use input configuration, host bindings and listeners, and injected dependencies; prefer declarative host APIs over direct DOM manipulation. Keep one focused responsibility and account for keyboard behavior, cleanup, and server-rendering compatibility.
- input and state
- component and template
- rendered Angular UI
Explain the core concept and Angular data flow of Custom Directives
Core syntax or pattern
// highlight.directive.ts
import { Directive, ElementRef, HostListener, Input } from '@angular/core';
@Directive({
selector: '[appHighlight]'
})
export class HighlightDirective {
@Input() highlightColor = 'yellow';
constructor(private el: ElementRef) { }
@HostListener('mouseenter') onMouseEnter() {
this.highlight(this.highlightColor);
}
@HostListener('mouseleave') onMouseLeave() {
this.highlight('');
}
private highlight(color: string) {
this.el.nativeElement.style.backgroundColor = color;
}
}
// Usage in template
<p appHighlight highlightColor="lightblue">
Hover over me!
</p>An attribute directive adds reusable behavior to an existing element or component. It can use input configuration, host bindings and listeners, and injected dependencies; prefer declarative host APIs over direct DOM manipulation. Keep one focused responsibility and account for keyboard behavior, cleanup, and server-rendering compatibility.
Read the code step by step
- input and state
// highlight.directive.tsCustom Directive s let you create your own logic to modify the behavior of DOM elements.
- component and template
@HostListener('mouseleave') onMouseLeave() {An attribute directive adds reusable behavior to an existing element or component. It can use input configuration, host bindings and listeners, and injected dependencies; prefer declarative host APIs over direct DOM manipulation. Keep one focused responsibility and account for keyboard behavior, cleanup, and server-rendering compatibility.
- rendered Angular UI
</p>Build an autofocus or highlight directive and test changing inputs, disabled state, keyboard interaction, and destruction cleanup.
- input and state
- component and template
- rendered Angular UI
Success
Explain the core concept and Angular data flow of Custom Directives
Failure
Unrestricted direct use of document or nativeElement can break SSR, testing, and security boundaries.
Custom Directives let you create your own logic to modify the behavior of DOM elements.
// highlight.directive.ts
import { Directive, ElementRef, HostListener, Input } from '@angular/core';
@Directive({
selector: '[appHighlight]'
})
export class HighlightDirective {
@Input() highlightColor = 'yellow';
constructor(private el: ElementRef) { }
@HostListener('mouseenter') onMouseEnter() {
this.highlight(this.highlightColor);
}
@HostListener('mouseleave') onMouseLeave() {
this.highlight('');
}
private highlight(color: string) {
this.el.nativeElement.style.backgroundColor = color;
}
}
// Usage in template
<p appHighlight highlightColor="lightblue">
Hover over me!
</p>(When you hover the mouse over the paragraph, a lightblue background appears)Real-world usage
How to apply it in a real project
Build an autofocus or highlight directive and test changing inputs, disabled state, keyboard interaction, and destruction cleanup.
Mini challenge
Build it yourself
Rebuild the Custom Directives example without copying it. Add two normal states, one edge case, and one failure state, and write the expected UI and state transition before running it.
- Rebuild the example without copying it
- Test a normal input and an edge case
- Predict the compiler or output result before running it
// highlight.directive.ts
import { Directive, ElementRef, HostListener, Input } from '@angular/core';
@Directive({
selector: '[appHighlight]'
})
export class HighlightDirective {
@Input() highlightColor = 'yellow';
constructor(private el: ElementRef) { }
@HostListener('mouseenter') onMouseEnter() {
this.highlight(this.highlightColor);
}
@HostListener('mouseleave') onMouseLeave() {
this.highlight('');
}
private highlight(color: string) {
this.el.nativeElement.style.backgroundColor = color;
}
}
// Usage in template
<p appHighlight highlightColor="lightblue">
Hover over me!
</p>Expected: Explain the core concept and Angular data flow of Custom Directives
Show hint
Build an autofocus or highlight directive and test changing inputs, disabled state, keyboard interaction, and destruction cleanup.
Quick quiz 1 / 3
Quick quiz 2 / 3
Quick quiz 3 / 3
What to remember
- input and state → component and template → rendered Angular UI
- An attribute directive adds reusable behavior to an existing element or component. It can use input configuration, host bindings and listeners, and injected dependencies; prefer declarative host APIs over direct DOM manipulation. Keep one focused responsibility and account for keyboard behavior, cleanup, and server-rendering compatibility.
- Unrestricted direct use of document or nativeElement can break SSR, testing, and security boundaries.
- Build an autofocus or highlight directive and test changing inputs, disabled state, keyboard interaction, and destruction cleanup.
Angular — Directives — Angular