Skip to main content
Pipes are simple functions used in templates to transform data for display. Angular provides built-in pipes for common transformations and allows you to create custom pipes for specific needs.

What are Pipes?

Pipes take data as input and transform it to a desired output format. They are used in template expressions:
Pipes are pure by default, meaning they only execute when their input reference changes. This ensures optimal performance.

Built-in Pipes

Angular provides several built-in pipes:

DatePipe

Format dates:

CurrencyPipe

Format currency values:

DecimalPipe

Format numbers:

PercentPipe

Format percentages:

UpperCasePipe and LowerCasePipe

Transform text case:

JsonPipe

Display objects for debugging:

SlicePipe

Extract a subset of array or string:

AsyncPipe

Automatically subscribe to observables:
The AsyncPipe automatically unsubscribes when the component is destroyed, preventing memory leaks.

Creating Custom Pipes

Implement the PipeTransform interface:

Pipe with Multiple Arguments

Impure Pipes

Pipes that detect all changes (use sparingly):
Impure pipes can impact performance because they execute on every change detection cycle. Use them carefully.

Chaining Pipes

Combine multiple pipes:

Complex Custom Pipe

Pipe with dependency injection:

Async Operations in Pipes

Best Practices

Keep Pipes Pure

Default pure pipes for better performance

Simple Transforms

Keep transformation logic simple

Use AsyncPipe

For observables to prevent memory leaks

Avoid Side Effects

Pipes should only transform data
  1. Keep pipes pure - Unless you have a specific reason
  2. Avoid complex logic - Move to services if needed
  3. Use meaningful names - Clear and descriptive
  4. Handle null/undefined - Defensive programming
  5. Document parameters - Clear usage examples
  6. Test thoroughly - Pipes are easy to unit test

Next Steps

Templates

Learn template syntax and bindings

Directives

Extend element behavior