Angular
Pipes & Custom Pipe
Transform template values with built-in and custom pipes.
By EZ4Code Team
pipestransform
Code
import { Pipe, PipeTransform } from "@angular/core";
@Pipe({ name: "truncate", standalone: true })
export class TruncatePipe implements PipeTransform {
transform(value: string, limit = 20): string {
if (value.length <= limit) return value;
return value.slice(0, limit) + "...";
}
}
// Usage in template:
// {{ "Hello Angular world" | truncate:5 }} -> "Hello..."
// {{ price | currency:"USD" }}
// {{ today | date:"short" }}
// {{ obj | json }}Explanation
Pipes transform template display values via the | operator. Angular ships built-in pipes like date, currency, uppercase, and json. Custom pipes implement PipeTransform and register a name for use in templates with optional arguments.
More Angular Snippets
Component Basics
Define an Angular component with selector, template, and styles.
Template Syntax & Binding
Interpolation, property, event, and two-way binding in Angular templates.
Built-in Directives
Use *ngIf, *ngFor, ngClass, and ngStyle to shape the DOM.
Services & Dependency Injection
Create an injectable service and consume it in a component.
Routing Configuration
Define routes with params, lazy loading, and redirects.
Reactive Forms
Build a typed reactive form with FormBuilder and validators.