Friday, 18 April 2025

Angular 19 | Chapter 4 | Pipes and Directives

 

In Angular, pipes and directives are two powerful features used to enhance templates and the behavior of DOM elements. Here's a breakdown of each:

 

✅ Pipes

Pipes are used to transform data in templates.
 

๐Ÿ“Œ What they do:

  • Take in data as input.
  • Transform it into a desired format.
  • Output the transformed data.
     

๐Ÿ”ง Common Use Cases:

  • Formatting dates, currency, or numbers.
  • Converting strings to uppercase/lowercase.
  • Creating custom transformations (e.g., filtering lists).
     

๐Ÿงช Example (built-in pipe):

<p>{{ birthday | date:'longDate' }}</p>

This will format the birthday variable using Angular’s built-in date pipe.
 

๐Ÿ› ️ Custom Pipe Example:

@Pipe({name: 'capitalize'})
export class CapitalizePipe implements PipeTransform {
  transform(value: string): string {
    return value.charAt(0).toUpperCase() + value.slice(1);
  }
}
 

Usage:

<p>{{ 'hello' | capitalize }}</p> <!-- Output: Hello -->

 

✅ Directives

Directives are used to change the appearance, behavior, or layout of DOM elements.

๐Ÿ“Œ Types of Directives:

  1. Structural Directives – change the DOM layout (e.g., *ngIf, *ngFor)
  2. Attribute Directives – change the appearance or behavior of an element (e.g., ngClass, ngStyle, custom ones)
     

๐Ÿงฑ Structural Directive Example:
 

<div *ngIf="isLoggedIn">Welcome back!</div>
 

๐ŸŽจ Attribute Directive Example:
 

<p [ngStyle]="{color: 'blue'}">This text is blue.</p>
 

๐Ÿ› ️ Custom Attribute Directive Example:
 

@Directive({
  selector: '[appHighlight]'
})
export class HighlightDirective {
  constructor(el: ElementRef) {
    el.nativeElement.style.backgroundColor = 'yellow';
  }
}

 

Usage:

<p appHighlight>This text has a yellow background.</p>

 

๐Ÿง  Summary

Feature

Purpose

Example

Pipe

Transforms data in templates

{{ 'hello' | capitalize }}

Directive

Changes DOM behavior/structure

<div *ngIf="show">

 

 

3 comments:

  1. Pipes and directives serve different but complementary purposes in Angular templates. Pipes focus on transforming data for presentation, while directives modify the appearance, behavior, or layout of DOM elements. The article provides clear examples of both built-in and custom implementations.

    The custom capitalize pipe demonstrates how reusable data transformations can be created when the built-in options are not sufficient. Developers following Angular Training can apply the same pattern to formatting and preparing values directly within templates.

    ReplyDelete
  2. The discussion of structural and attribute directives also makes the distinction between changing DOM layout and modifying element behavior easier to understand. This is particularly useful when learning TypeScript Training, since the custom HighlightDirective example shows how Angular classes can interact with DOM elements.

    ReplyDelete
  3. The combination of pipes for presentation and directives for DOM behavior provides a useful foundation for building reusable Angular components. These concepts can be applied directly in Angular Projects where template formatting, conditional rendering, and custom element behavior are commonly required.

    ReplyDelete