Sub topic 2
Passing parameters to pipes
This chapter assumes you know basic TypeScript, Angular components with template interpolation, and how a standalone component lists its dependencies in imports. You have already seen the pipe operator |. Here you will learn how to configure a pipe with parameters.
Why pipes take parameters
A pipe with no parameters uses its defaults. Most built-in pipes also accept options, so one pipe can produce many outputs. The date pipe, for example, can show a short date, a long date, or a time, depending on a format you pass.
You pass a parameter after a colon that follows the pipe name. Separate several parameters with more colons. The order is fixed by the pipe's transform method, so you must pass them in that order.
<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>Each line shows a different case:
date:'mediumDate'passes one parameter, a named format. The result looks likeJan 15, 2024in the default locale.currency:'EUR':'symbol':'1.2-2'passes three parameters: the currency code, how to display it, and the digits format.percent:'1.0-1'passes a digits format. A ratio of0.256becomes25.6%.titlecasetakes no parameters, so it has no colon.
Reading the digits-info string
The number-related pipes use a string in the form minInt.minFrac-maxFrac. It controls how many digits appear.
| Part | Meaning |
|---|---|
| `minInt` | Minimum number of integer digits. Shorter numbers are padded with zeros. |
| `minFrac` | Minimum number of fraction digits. Shorter fractions are padded with zeros. |
| `maxFrac` | Maximum number of fraction digits. Longer fractions are rounded. |
So '1.2-2' means at least one integer digit and exactly two fraction digits. The value 5 becomes 5.00, and 5.678 becomes 5.68. The string '3.0-0' pads 7 to 007.
Parameters can be expressions
A parameter is a template expression, not only a literal. You can bind it to a component property. This is useful when the format depends on user choice or configuration.
import { Component } from '@angular/core';
import { CurrencyPipe, DatePipe } from '@angular/common';
@Component({
selector: 'app-price-card',
standalone: true,
imports: [CurrencyPipe, DatePipe],
template: `
<p>{{ price | currency:currencyCode:'symbol':'1.2-2' }}</p>
<p>{{ createdAt | date:dateFormat }}</p>
`,
})
export class PriceCardComponent {
price = 1234.5;
currencyCode = 'USD';
createdAt = new Date(2024, 0, 15);
dateFormat = 'fullDate';
}Standalone components must import each pipe they use. If you forget CurrencyPipe, Angular reports that the pipe currency could not be found.
Skipping a middle parameter
Parameters are positional, so you cannot skip one. To change a later parameter, pass a value for every earlier one. If you want the default for an earlier parameter, pass undefined.
<p>{{ createdAt | date:'short':undefined:'en-GB' }}</p>Here the date pipe receives the format 'short', the default time zone through undefined, and the locale 'en-GB'.
How the parts fit together
Common errors
- Wrong order.
currency:'symbol':'EUR'treats'symbol'as the currency code and gives unexpected output. Check the pipe's documented parameter order. - Missing quotes.
date:mediumDatelooks up a component property namedmediumDate. Quote string literals. - Invalid format strings. A digits-info string such as
'2-3'without the integer part throws a runtime error. Use the fullminInt.minFrac-maxFracform. - Forgetting the import. In a standalone component, a pipe that is not in
importsfails at compile time.
Exercise
A template contains {{ amount | currency:'symbol':'USD' }}. The output is not what the author wanted. What is wrong, and how do you fix it so that 5 displays as a US dollar amount with exactly two decimals?
Show answer
The first parameter of currency is the currency code, and the second is the display mode, so the two are swapped. Write {{ amount | currency:'USD':'symbol':'1.2-2' }}. The value 5 then displays as $5.00.
Exercise
How do you set the locale of the date pipe to 'fr' for a single binding while keeping the default format and time zone?
Show answer
Pass undefined for the earlier parameters: {{ createdAt | date:undefined:undefined:'fr' }}. The locale 'fr' must also be registered with registerLocaleData first.