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:
- Structural
Directives
– change the DOM layout (e.g., *ngIf, *ngFor)
- 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"> |
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.
ReplyDeleteThe 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.
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.
ReplyDeleteThe 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