Thuta Learning
Angular
IntermediateWeb DevelopmentIntermediate

Custom Directives

AngularLesson 1720 min

What you'll walk away with

  • Explain the core concept and Angular data flow of Custom Directives
  • Run the example, predict its state and UI, and test edge and error cases
  • Apply a typed safe pattern in a real project and recognize common pitfalls

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.

Mental model
  1. input and state
  2. component and template
  3. rendered Angular UI

Explain the core concept and Angular data flow of Custom Directives

Core syntax or pattern

typescript
// 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

  1. input and state// highlight.directive.ts

    Custom Directive s let you create your own logic to modify the behavior of DOM elements.

  2. 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.

  3. rendered Angular UI</p>

    Build an autofocus or highlight directive and test changing inputs, disabled state, keyboard interaction, and destruction cleanup.

Execution flow
  1. input and state
  2. component and template
  3. 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.

typescript
// 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>
You should see
(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
typescript
// 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

What best describes the core idea of Custom Directives?

Quick quiz 2 / 3

Which mistake should you avoid first?

Quick quiz 3 / 3

How should this be applied in a real project?

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

Easy traps

  • Unrestricted direct use of document or nativeElement can break SSR, testing, and security boundaries.
  • One happy-path result does not prove the feature works; empty, loading, error, and cleanup behavior need explicit tests.

Hands-on Exercise

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.

You'll know it worked when: (When you hover the mouse over the paragraph, a lightblue background appears)

Updated on

Progress is saved only in this browser on this device. It isn't synced to other devices.

Report a problem with this lessonOpens your email app with this lesson's address filled in — nothing is sent until you send it.

Custom Directives | Thuta Learning