Sub topic 1
What Pipes Are and How to Apply Them
Prerequisites
This chapter assumes basic TypeScript, Angular component templates, interpolation, and standalone component imports.
What Pipes Are and How to Apply Them
An Angular pipe transforms a value for display in a template. The pipe does not change the component's stored value. Use the pipe operator, |, inside an interpolation:
<p>{{ name | uppercase }}</p>Angular passes name to the uppercase pipe and displays the returned text. The component can keep the original value, while the template controls its presentation.
Standalone components must import the pipes they use. CommonModule provides Angular's common built-in pipes:
import { CommonModule } from '@angular/common';
import { Component } from '@angular/core';
@Component({
selector: 'app-profile',
standalone: true,
imports: [CommonModule],
template: `<p>{{ name | uppercase }}</p>`,
})
export class ProfileComponent {
name = 'Ada Lovelace';
}A common error is expecting a pipe to update the underlying property. Pipes are for presentation. Use component or service logic when the application needs changed data.
Passing Parameters to Pipes
A pipe parameter follows the pipe name and a colon:
<p>{{ createdAt | date:'mediumDate' }}</p>
<p>{{ price | currency:'EUR':'symbol':'1.2-2' }}</p>
<p>{{ ratio | percent:'1.0-1' }}</p>Parameters customize formatting. For currency, the arguments select the currency code, display style, and minimum-maximum digit pattern. A digit pattern such as 1.2-2 means at least one integer digit and exactly two fraction digits.
Chaining Pipes
You can apply several pipes in one expression. Angular evaluates them from left to right:
<p>{{ createdAt | date:'fullDate' | uppercase }}</p>First, date returns formatted text. Then uppercase receives that text. Ensure each pipe accepts the previous pipe's output. A date pipe followed by a text pipe works because both produce and consume strings. Chaining a numeric operation after a text-formatting pipe may produce an invalid result.
Changing Text Case
Use uppercase, lowercase, and titlecase for common text presentation:
<p>{{ username | lowercase }}</p>
<p>{{ heading | titlecase }}</p>titlecase capitalizes words for display. It is not a full grammar-aware capitalization tool, so names or sentences with special capitalization may need explicit handling.
Exercise
What does {{ 'hello angular' | uppercase | titlecase }} display?
Show answer
Hello Angular. The uppercase pipe runs first, then titlecase converts the result.