Sub topic 12
Formatting Values with Pipes
This chapter assumes you know text interpolation and how a standalone component's imports array works. A pipe is a function you use inside a template expression to change how a value is displayed. The value in your class stays the same.
Using Built-in Pipes
You apply a pipe with the | operator. You pass arguments after a colon. @angular/common provides the common ones: DatePipe, CurrencyPipe, DecimalPipe, PercentPipe, UpperCasePipe, LowerCasePipe, JsonPipe, and AsyncPipe. A pipe is a dependency of your template, so you import it the same way you import a component.
import { Component, signal } from '@angular/core';
import { CurrencyPipe, DatePipe, PercentPipe, UpperCasePipe } from '@angular/common';
@Component({
selector: 'app-order',
imports: [CurrencyPipe, DatePipe, PercentPipe, UpperCasePipe],
template: `
<p>{{ customer() | uppercase }}</p>
<p>Total: {{ total() | currency: 'EUR' }}</p>
<p>Discount: {{ discount() | percent: '1.0-1' }}</p>
<p>Placed: {{ placed() | date: 'medium' | uppercase }}</p>
`
})
export class OrderComponent {
customer = signal('Ada Lovelace');
total = signal(42.5);
discount = signal(0.125);
placed = signal(new Date());
}The last line chains two pipes. They run from left to right. date turns the Date into a string, and then uppercase changes that string.
A pipe has lower precedence than most operators. a + b | currency formats the sum. With a ternary, put parentheses around the pipe part: cond ? (x | currency) : 'n/a'.
Writing a Custom Pipe
A custom pipe is a class with the @Pipe decorator that implements PipeTransform. In the template, the value on the left of | becomes the first argument of transform, and each argument after a colon becomes the next parameter. Pipes are standalone by default.
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({ name: 'truncate' })
export class TruncatePipe implements PipeTransform {
transform(value: string, limit = 20, suffix = '…'): string {
return value.length > limit ? value.slice(0, limit) + suffix : value;
}
}To use it, add TruncatePipe to the component's imports, then write {{ description() | truncate: 10 }} or {{ description() | truncate: 10 : '...' }}.
Pure Pipes and Common Errors
Pipes are pure by default. Angular calls transform again only when an input changes identity: a new primitive value or a new object reference. Because of this, pure pipes are cheap. It also causes the most common bug.
Other frequent mistakes:
| Symptom | Cause |
|---|---|
| `No pipe found with name 'currency'` (NG0302) | The pipe class is missing from `imports` |
| Wrong currency or date format | Pass a currency code, a format, or a locale explicitly |
| Heavy logic slows the page | Move the logic into a `computed()` signal or keep the pipe pure |
Exercise
You write {{ price() | currency }} in a standalone component and get "No pipe found with name 'currency'". What fixes it, and how would you show the value in US dollars with no decimal places?
Show answer
Import CurrencyPipe from @angular/common and add it to the component's imports array. Then write {{ price() | currency: 'USD' : 'symbol' : '1.0-0' }}. The digit-info string '1.0-0' means at least one integer digit and exactly zero fraction digits.
Exercise
A pure sortBy pipe displays items. After this.items().push(newItem), the list on screen does not change. Why, and what is the fix?
Show answer
A pure pipe runs again only when its input reference changes. push mutates the same array, so the pipe never sees a new input. Update the signal with a new array instead: this.items.update(list => [...list, newItem]).