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.- Syntax
- Examples
- Format Options
- Configuration
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.- Syntax
- Examples
- Component
packages/common/src/pipes/number_pipe.ts:220
Async Data
AsyncPipe
Unwraps a value from an asynchronous primitive (Observable or Promise).- Syntax
- Observable Example
- Promise Example
- With ngIf
packages/common/src/pipes/async_pipe.ts:144
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
nulluntil the first value is emitted
Data Transformation
JsonPipe
Converts a value into its JSON-format representation.- Syntax
- Examples
- Component
packages/common/src/pipes/json_pipe.ts:33
Additional Pipes
The Common package includes many other useful pipes:DecimalPipe (number)
DecimalPipe (number)
Formats numbers according to locale rules.
PercentPipe (percent)
PercentPipe (percent)
Transforms a number to a percentage string.
SlicePipe (slice)
SlicePipe (slice)
Creates a new Array or String containing a subset of elements.
KeyValuePipe (keyvalue)
KeyValuePipe (keyvalue)
Transforms Object or Map into an array of key-value pairs.
Case Conversion Pipes
Case Conversion Pipes
Transform text case:
I18n Pipes
I18n Pipes
Internationalization pipes:
I18nPluralPipe- Maps a value to a string that pluralizes the valueI18nSelectPipe- Maps a string value to another string
Usage Patterns
Chaining Pipes
Pipes can be chained together:Parameterized Pipes
Many pipes accept parameters: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:Importing Pipes
Performance Tips
Use Pure Pipes
Use Pure Pipes
Pure pipes are more performant. Only use impure pipes (
pure: false) when necessary.Avoid Heavy Computations
Avoid Heavy Computations
Pipes are called frequently. Keep transformations lightweight or cache results.
Async Pipe Best Practices
Async Pipe Best Practices
- Use with
*ngIfor@ifto 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
