Skip to the content.

πŸ“˜ Angular > Directives

πŸ—οΈ Angular Directives

Directives in Angular are used to extend the functionality of HTML by adding custom behavior to elements. They allow you to manipulate the DOM, apply logic, and dynamically update the UI.


πŸ”„ Built-in Directives

Angular provides several built-in directives to simplify common tasks. These directives are categorized into Structural Directives and Attribute Directives.

1. Structural Directives

Structural directives change the structure of the DOM by adding or removing elements.

2. Attribute Directives

Attribute directives modify the behavior or appearance of an element.


πŸ› οΈ Creating a Custom Directive

In addition to built-in directives, Angular allows you to create custom directives to add specific behavior to elements.

πŸ“œ Steps to Create a Directive

  1. Use the Angular CLI to generate a directive:
    ng generate directive my-directive
    
  2. This command will create the following files:
    • my-directive.directive.ts: Contains the directive logic.
    • my-directive.directive.spec.ts: Contains unit tests for the directive.
  3. Implement the directive logic in the .directive.ts file.

πŸ“„ Example: A Custom Highlight Directive

Here’s an example of a custom directive that highlights an element on hover:

Directive File (highlight.directive.ts):

import { Directive, ElementRef, Renderer2, HostListener } from '@angular/core';

@Directive({
  selector: '[appHighlight]' // Custom directive selector
})
export class HighlightDirective {
  constructor(private el: ElementRef, private renderer: Renderer2) {}

  @HostListener('mouseenter') onMouseEnter() {
    this.renderer.setStyle(this.el.nativeElement, 'backgroundColor', 'yellow');
  }

  @HostListener('mouseleave') onMouseLeave() {
    this.renderer.removeStyle(this.el.nativeElement, 'backgroundColor');
  }
}

Using the Directive in a Template (app.component.html):

<p appHighlight>Hover over this text to see the highlight effect.</p>

πŸ”„ Lifecycle Hooks in Directives

Custom directives can also use lifecycle hooks to manage their behavior:

Example:

import { Directive, OnInit, OnDestroy } from '@angular/core';

@Directive({
  selector: '[appExample]'
})
export class ExampleDirective implements OnInit, OnDestroy {
  ngOnInit() {
    console.log('Directive initialized');
  }

  ngOnDestroy() {
    console.log('Directive destroyed');
  }
}

Understanding both built-in and custom directives is essential for creating dynamic and interactive user interfaces in Angular applications.