Pipes & Directives in Angular
From the Angular cheat sheet · Advanced Features · verified Jul 2026
Pipes & Directives
Custom pipes and directives
typescript
// Custom pipe
@Pipe({ name: 'truncate' })
export class TruncatePipe implements PipeTransform {
transform(value: string, limit = 100): string {
return value.length > limit
? value.substring(0, limit) + '...'
: value;
}
}
// Usage
{{ longText | truncate:50 }}
// Custom directive
@Directive({ selector: '[appHighlight]' })
export class HighlightDirective {
@Input() appHighlight = 'yellow';
@HostListener('mouseenter') onMouseEnter() {
this.el.nativeElement.style.backgroundColor = this.appHighlight;
}
constructor(private el: ElementRef) {}
}💡 Keep pipes pure for better performance
⚡ Use trackBy in ngFor for large lists
📌 Directives add behavior to existing elements
🔥 OnPush change detection optimizes performance