Sub topic 3
Chaining pipes
This chapter assumes you can write a standalone Angular component and use {{ }} interpolation. It also assumes you know how to pass a parameter to a pipe, such as date:'mediumDate'.
How chaining works
You can put several pipes in one expression by separating them with |. Angular evaluates them left to right. The output of each pipe becomes the input of the next one.
<p>{{ createdAt | date:'fullDate' | uppercase }}</p>Here createdAt is a Date. The date pipe turns it into a string, and uppercase receives that string. For 15 January 2024 with the default en-US locale, the output is MONDAY, JANUARY 15, 2024.
Each pipe in a chain can have its own parameters. Parameters belong to the pipe they follow, so they do not affect the other pipes in the chain.
A complete example
import { Component } from '@angular/core';
import { DatePipe, SlicePipe, UpperCasePipe } from '@angular/common';
@Component({
selector: 'app-post-header',
standalone: true,
imports: [DatePipe, SlicePipe, UpperCasePipe],
template: `
<h2>{{ title | slice:0:12 | uppercase }}</h2>
<p>{{ createdAt | date:'fullDate' | uppercase }}</p>
`,
})
export class PostHeaderComponent {
title = 'Pipes in Angular';
createdAt = new Date(2024, 0, 15);
}The title is cut to its first 12 characters, Pipes in Ang, and then upper-cased. A standalone component must list every pipe it uses in imports, including built-in ones. A missing import causes a compile error saying the pipe could not be found.
Order matters
Each pipe expects a particular input type, so the order is part of the logic. This chain fails:
<p>{{ createdAt | uppercase | date:'fullDate' }}</p>uppercase receives a Date instead of a string, and it throws an InvalidPipeArgument error. The date pipe has to run first because it produces the string.
Order can also change the result when both orders are valid. Compare these two:
<p>{{ name | slice:0:3 | titlecase }}</p>
<p>{{ name | titlecase | slice:0:3 }}</p>With name = 'ada lovelace', both give Ada. With a value such as 'mcDONALD', the result depends on which step shapes the text first. When you are unsure, read the chain from left to right and write down the type after each step.
Precedence and parentheses
The pipe operator has lower precedence than almost every other operator, including the conditional ? :. This expression does not do what it appears to do:
<p>{{ isDraft ? draftDate : publishedDate | date:'shortDate' }}</p>The pipe binds only to publishedDate. When isDraft is true, draftDate is displayed unformatted. Wrap the condition in parentheses to format both branches:
<p>{{ (isDraft ? draftDate : publishedDate) | date:'shortDate' }}</p>You need the same parentheses when a pipe appears inside a larger expression. {{ (price | currency:'EUR') + ' net' }} formats the price before joining the text.
Chaining with the async pipe
The async pipe usually comes first in a chain, because it supplies the value for the rest of the chain. Until the Observable emits, async returns null. The pipes after it must accept null, and date and slice do.
<p>{{ createdAt$ | async | date:'shortDate' }}</p>While the Observable has not emitted, this prints nothing. After the first emission, it prints the formatted date.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `InvalidPipeArgument` at runtime | A pipe received a type it does not accept, often from wrong order | Put the type-converting pipe first |
| Pipe applies to only part of an expression | Operator precedence | Add parentheses |
| `The pipe 'uppercase' could not be found` | The pipe is not in the component's `imports` | Import the pipe class |
| Blank output at first with `async` | The value is `null` until the first emission | Expected. Use `@if` with `as` for a placeholder |
Keeping chains readable
A chain of two or three pipes is easy to read. Longer chains usually mean the template is doing work that belongs elsewhere. If you repeat the same chain in several templates, write a custom pipe that wraps it. You will do that in a later chapter.
Exercise
A template contains {{ isNew ? newDate : oldDate | date:'mediumDate' | uppercase }}. When isNew is true, the date appears unformatted. Why, and how do you fix it?
Show answer
The pipe operator has lower precedence than the conditional operator, so the pipes bind only to oldDate. Wrap the condition in parentheses: {{ (isNew ? newDate : oldDate) | date:'mediumDate' | uppercase }}. Both branches then pass through date and uppercase.
Exercise
Why does {{ createdAt | uppercase | date:'fullDate' }} throw an error when createdAt is a Date?
Show answer
Chained pipes run left to right, so uppercase receives the Date object. It accepts only strings and throws InvalidPipeArgument. Put date first so that uppercase receives the formatted string.