Sub topic 1
What Pipes Are and How to Apply Them
Prerequisites
This chapter assumes you know basic TypeScript syntax. It also assumes you can write an Angular standalone component, use interpolation ({{ }}) in a template, and list dependencies in a component's imports array.
What a pipe is
A pipe is a small function that takes a value in a template and returns a display-ready version of it. The component keeps the raw data, and the template decides how that data looks. A date stays a Date object in your class, and the template turns it into "Jan 15, 2024".
You apply a pipe with the pipe operator, |. The value goes on the left and the pipe name goes on the right.
<p>{{ name | titlecase }}</p>If name is 'ada lovelace', the page shows Ada Lovelace. The name property is not changed. Only the displayed text is.
Why use pipes
- The component holds one raw value, so sorting, comparing and saving work on clean data.
- Formatting logic lives in one place and is reused across templates.
- Angular ships built-in pipes for common jobs, such as dates, numbers, currency and text case.
How a value flows through a pipe
Every pipe is a class with a transform method. The template syntax value | pipeName makes Angular call transform(value) and display the return value.
Passing parameters
Many pipes accept parameters. You write each one after a colon. Parameters are given in the order the pipe's transform method expects them.
<p>{{ createdAt | date:'mediumDate' }}</p>
<p>{{ price | currency:'EUR':'symbol':'1.2-2' }}</p>
<p>{{ ratio | percent:'1.0-1' }}</p>
<p>{{ name | titlecase }}</p>Here is what each line does:
date:'mediumDate'picks a named date format.currency:'EUR':'symbol':'1.2-2'sets the currency code, shows the symbol (€), and requires exactly two decimal places.percent:'1.0-1'shows at least one integer digit and between zero and one decimal digit.titlecasetakes no parameters, so there is no colon.
The digits string has the form minInteger.minFraction-maxFraction. Later chapters cover the date, number and currency pipes in detail.
Chaining pipes
You can put several pipes in one expression. Angular applies them from left to right, and each pipe receives the result of the previous one.
<p>{{ createdAt | date:'fullDate' | uppercase }}</p>The date pipe first turns the Date into a string such as Monday, January 15, 2024. The uppercase pipe then receives that string and returns MONDAY, JANUARY 15, 2024. Order matters. Reversing the two would pass a Date to uppercase, which expects a string.
Using a pipe in a component
Pipes are not available in a template until the component imports them. Pipes like date, currency and titlecase are standalone, so you list them in imports.
import { Component } from '@angular/core';
import { DatePipe, TitleCasePipe } from '@angular/common';
@Component({
selector: 'app-profile',
standalone: true,
imports: [DatePipe, TitleCasePipe],
template: `
<p>{{ name | titlecase }}</p>
<p>{{ joined | date:'mediumDate' }}</p>
`,
})
export class ProfileComponent {
name = 'ada lovelace';
joined = new Date(2024, 0, 15);
}Common errors
- Missing import. If you see
No pipe found with name 'date', the pipe class is missing from the component'simportsarray. - Wrong parameter order.
currency:'symbol':'EUR'swaps the currency code and display mode. Check the pipe's documentation for the order. - Forgotten quotes.
date:mediumDatelooks for a component property calledmediumDate. Writedate:'mediumDate'for a string. - Expression precedence. The pipe operator has lower precedence than most operators. In a conditional such as
a ? b : c | uppercase, the pipe applies only toc. Wrap the whole expression, as in(a ? b : c) | uppercase.
Exercise
A component has title = 'angular pipes' and published = new Date(2024, 0, 15). Write one template expression that shows the published date as a full date in uppercase, and say what order the pipes run in.
Show answer
{{ published | date:'fullDate' | uppercase }}. Pipes run left to right. date formats the Date first and returns a string. uppercase then converts that string, giving MONDAY, JANUARY 15, 2024. Both DatePipe and UpperCasePipe must be in the component's imports array.