What are Pipes?
Pipes are simple functions designed to accept an input value and transform it into a desired output for display in the UI. As a Technical Lead, you should use pipes to keep your component logic clean; instead of formatting a date or currency in your TypeScript class, you do it directly in the template. This follows the principle of separating 'Data' from 'Presentation'.
- Using Built-in Pipes
Angular comes with several built-in pipes for common data transformations. You use the 'pipe' operator (|) followed by the pipe name and any optional parameters (separated by colons).
- Chaining Pipes
You can chain multiple pipes together to perform complex transformations in a single line. The output of the first pipe becomes the input for the next.
The Data Transformation Flow
Pipes act as a middleman between your raw data and the final rendered view.
- The Async Pipe (Architect's Favorite)
For high-performance applications like Revochamp, the async pipe is essential. It automatically subscribes to an Observable or Promise and returns the latest value it has emitted. Most importantly, it automatically unsubscribes when the component is destroyed to prevent memory leaks.
Common Pipes Reference
| Pipe | Purpose | Example Output |
|---|---|---|
| DatePipe | Formats date objects/strings | Apr 5, 2026 |
| UpperCasePipe | Converts text to uppercase | REVOCHAMP |
| CurrencyPipe | Formats numbers as currency | $100.00 |
| DecimalPipe | Formats numbers with decimals | 1,234.56 |
| JsonPipe | Converts object to JSON string | { "id": 1 } |
| AsyncPipe | Handles Observables/Promises | (Stream value) |