Mastering Angular Pipes: Transforming Data with Ease
Learn how to leverage Angular's built-in and custom pipes to transform and display data in your applications efficiently. Discover practical examples and tips for creating powerful data transformations and optimizing performance.
Introduction
In Angular, pipes are a powerful feature that allows developers to transform data at the view level, making it easier to display information in the desired format. Whether it's converting text to uppercase, formatting dates, or transforming objects into key-value pairs, Angular's built-in pipes provide a range of tools to streamline data presentation. Additionally, developers can create custom pipes to meet specific needs, enhancing the flexibility and functionality of their applications.
Understanding Pipe Syntax
The basic syntax of a pipe involves using the pipe name followed by the expression we want to transform, separated by the pipe symbol (|):
{{ expression | pipeName }}
Parameters can be added to the pipe name, separated by colons:
{{ expression | pipeName:param }}
Pipes can be chained together, allowing for complex transformations.
Built-in Pipes in Angular
Angular comes with a variety of built-in pipes that cover common data transformation needs:
uppercase/lowercase: Converts text to uppercase or lowercase.
percent: Formats numbers as percentages.
date: Formats dates according to locale settings or specified formats.
currency: Formats numbers as currency, with customizable currency symbols.
json: Converts objects to JSON format for debugging.
keyvalue: Transforms objects into key-value pairs.
slice: Extracts a subset of an array or string.
async: Handles asynchronous data, updating views with new data as it arrives.
Practical Examples
Let's explore practical examples of using some of these pipes in an Angular application.
Displaying Product Prices
To display the price of a selected product in euros, we can use the currency pipe:
<p>{{ product.price | currency:'EUR' }}</p>
Showing Product Categories
To display product categories using the key-value and lowercase pipes, we can iterate over the categories object:
<div class="pill-group">
<div *ngFor="let category of product.categories | keyvalue">
<p class="pill">{{ category.value | lowercase }}</p>
</div>
</div>
Creating Custom Pipes
While built-in pipes are useful for many scenarios, there are times when custom transformations are necessary. For example, to sort a list of products by title, we can create a custom pipe.
First, generate the pipe using Angular CLI:
ng generate pipe sort
Next, implement the pipe logic in sort.pipe.ts:
import { Pipe, PipeTransform } from '@angular/core';
import { Product } from './product';
@Pipe({
name: 'sort',
standalone: true
})
export class SortPipe implements PipeTransform {
transform(value: Product[]): Product[] {
if (!value) return [];
return value.sort((a, b) => a.title.localeCompare(b.title));
}
}
Using the Custom Pipe
To use the custom sort pipe in a component template, import it and apply it to the product list:
<ul>
<li *ngFor="let product of products | sort">{{ product.title }}</li>
</ul>
Pure vs. Impure Pipes
Pipes in Angular can be pure or impure. Pure pipes only execute when their input changes by reference, making them efficient for most use cases. However, impure pipes, can be set by configuring the pure property to false, execute on every change detection cycle. Use impure pipes cautiously to avoid performance issues.
Conclusion Angular pipes are a versatile tool for transforming and displaying data. By mastering both built-in and custom pipes, developers can create efficient, readable, and maintainable code. Whether formatting dates, currencies, or custom sorting lists, pipes simplify data manipulation in Angular applications.


