Skip to main content

Common Pipes

Angular’s @angular/common package provides essential pipes for transforming and formatting data in your templates. Pipes are pure functions that transform data for display without modifying the original data.

Date and Time

DatePipe

Formats a date value according to locale rules.
@Pipe({
  name: 'date'
})
export class DatePipe implements PipeTransform {
  transform(
    value: Date | string | number | null | undefined,
    format?: string,
    timezone?: string,
    locale?: string
  ): string | null;
}
Import:
import { DatePipe } from '@angular/common';
Source: packages/common/src/pipes/date_pipe.ts:224
Only the en-US locale data comes with Angular by default. To format dates in other languages, you must import the corresponding locale data.

Number Formatting

CurrencyPipe

Transforms a number to a currency string, formatted according to locale rules.
@Pipe({
  name: 'currency'
})
export class CurrencyPipe implements PipeTransform {
  transform(
    value: number | string | null | undefined,
    currencyCode?: string,
    display?: 'code' | 'symbol' | 'symbol-narrow' | string | boolean,
    digitsInfo?: string,
    locale?: string
  ): string | null;
}
Import:
import { CurrencyPipe } from '@angular/common';
Source: packages/common/src/pipes/number_pipe.ts:220
Configure the default currency code using the DEFAULT_CURRENCY_CODE injection token.

Async Data

AsyncPipe

Unwraps a value from an asynchronous primitive (Observable or Promise).
@Pipe({
  name: 'async',
  pure: false
})
export class AsyncPipe implements OnDestroy, PipeTransform {
  transform<T>(obj: Observable<T> | Subscribable<T> | PromiseLike<T> | null | undefined): T | null;
}
Import:
import { AsyncPipe } from '@angular/common';
Source: packages/common/src/pipes/async_pipe.ts:144
The async pipe automatically subscribes and unsubscribes, preventing memory leaks. It marks the component for change detection when new values are emitted.
Key Features:
  • Automatically subscribes to Observables
  • Automatically unsubscribes on component destruction
  • Handles both Observables and Promises
  • Marks component for change detection on value updates
  • Returns null until the first value is emitted

Data Transformation

JsonPipe

Converts a value into its JSON-format representation.
@Pipe({
  name: 'json',
  pure: false
})
export class JsonPipe implements PipeTransform {
  transform(value: any): string;
}
Import:
import { JsonPipe } from '@angular/common';
Source: packages/common/src/pipes/json_pipe.ts:33
The JSON pipe is particularly useful for debugging during development. Use it with the <pre> tag for formatted output.

Additional Pipes

The Common package includes many other useful pipes:
Formats numbers according to locale rules.
{{ 3.14159 | number }}              <!-- 3.142 -->
{{ 3.14159 | number:'1.0-0' }}      <!-- 3 -->
{{ 3.14159 | number:'3.1-5' }}      <!-- 003.14159 -->
{{ 1234.56 | number:'1.2-2':'fr' }} <!-- 1 234,56 -->
Transforms a number to a percentage string.
{{ 0.26 | percent }}          <!-- 26% -->
{{ 0.26 | percent:'2.2-2' }}  <!-- 26.00% -->
{{ 1.3495 | percent:'2.0-0' }} <!-- 135% -->
Creates a new Array or String containing a subset of elements.
{{ [1, 2, 3, 4, 5] | slice:1:3 }}     <!-- [2, 3] -->
{{ 'abcdefg' | slice:0:3 }}           <!-- 'abc' -->
{{ items | slice:0:5 }}               <!-- First 5 items -->
Transforms Object or Map into an array of key-value pairs.
<div *ngFor="let item of object | keyvalue">
  {{ item.key }}: {{ item.value }}
</div>
Transform text case:
{{ 'hello' | uppercase }}     <!-- HELLO -->
{{ 'WORLD' | lowercase }}     <!-- world -->
{{ 'john doe' | titlecase }}  <!-- John Doe -->
Internationalization pipes:
  • I18nPluralPipe - Maps a value to a string that pluralizes the value
  • I18nSelectPipe - Maps a string value to another string

Usage Patterns

Chaining Pipes

Pipes can be chained together:
{{ user$ | async | json }}
{{ dateValue | date:'short' | uppercase }}
{{ price | currency:'USD' | slice:0:5 }}

Parameterized Pipes

Many pipes accept parameters:
{{ value | pipeName:parameter1:parameter2:parameter3 }}
{{ date | date:'fullDate':'UTC':'en-US' }}
{{ amount | currency:'EUR':'symbol':'1.2-2':'fr' }}

Pure vs Impure Pipes

Pure Pipes (Default)

  • Called only when input value changes
  • Better performance
  • Examples: date, currency, uppercase

Impure Pipes

  • Called on every change detection cycle
  • Use sparingly for performance
  • Examples: async, json

Creating Custom Pipes

You can create custom pipes for specific transformation needs:
import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
  name: 'exponential',
  standalone: true
})
export class ExponentialPipe implements PipeTransform {
  transform(value: number, exponent = 1): number {
    return Math.pow(value, exponent);
  }
}
Usage:
{{ 2 | exponential:3 }}  <!-- 8 -->

Importing Pipes

import { CommonModule } from '@angular/common';

@Component({
  imports: [CommonModule],
  // ...
})

Performance Tips

Pure pipes are more performant. Only use impure pipes (pure: false) when necessary.
Pipes are called frequently. Keep transformations lightweight or cache results.
  • Use with *ngIf or @if to avoid multiple subscriptions
  • Prefer over manual subscribe/unsubscribe
  • Automatically handles memory cleanup

See Also

Pipes Guide

Learn about creating custom pipes

Directives

Common directives reference

Internationalization

i18n and localization

RxJS

Reactive programming with Observables