← Back to Angular, page 1

angular-0010 · Easy fresher

What are pipes in Angular, and how do you create a custom pipe?

A pipe transforms a value inside a template, for example formatting a date or a currency amount, with the syntax value | pipeName:arg. Built-in pipes cover common cases, and a custom pipe is a class with @Pipe and a transform method. Pipes are pure by default, so they re-run only when their input changes.

Full answer

A pipe takes a value, changes how it is shown, and returns the result. It does not change the original data. The syntax is {{ value | pipeName:argument }}, and pipes can be chained: {{ name | lowercase | titlecase }}.

Common built-in pipes: date, currency, number, percent, uppercase, lowercase, titlecase, json, slice, keyvalue, and async. Each is a standalone pipe, so you add the ones you use to imports.

To write your own, create a class with @Pipe({ name: 'grade' }) and a transform(value, ...args) method. Implement PipeTransform so TypeScript checks the signature. The pipe is then added to imports of the component that uses it.

Pure and impure pipes:

  • A pure pipe (the default) runs only when its input value or arguments change by reference or primitive value. This makes it cheap.
  • An impure pipe (pure: false) runs on every change detection pass. Use it rarely. async is the main built-in example, because it must react to a stream.

Because a pure pipe is checked by reference, passing the same array after push will not re-run a pure pipe that filters the array. Replace the array with a new one, or use a computed signal instead of a filtering pipe.

Pipes are good for display formatting. Do not put heavy filtering or sorting in them, and do not put business rules in them.

typescript
import { CurrencyPipe, DatePipe, UpperCasePipe } from '@angular/common';
import { Component, Pipe, PipeTransform } from '@angular/core';

@Pipe({ name: 'grade' })
export class GradePipe implements PipeTransform {
  transform(marks: number, passMark = 35): string {
    if (marks >= 75) return 'Distinction';
    if (marks >= 60) return 'First class';
    return marks >= passMark ? 'Pass' : 'Fail';
  }
}

@Component({
  selector: 'app-report',
  imports: [GradePipe, DatePipe, CurrencyPipe, UpperCasePipe],
  template: `
    <p>{{ student | uppercase }} got {{ marks | grade }}</p>
    <p>Exam date: {{ examDate | date: 'dd MMM yyyy' }}</p>
    <p>Fee: {{ fee | currency: 'INR' : 'symbol' : '1.0-0' }}</p>
  `,
})
export class ReportComponent {
  student = 'asha';
  marks = 68;
  examDate = new Date(2026, 11, 4);
  fee = 12500;
}

Angular 20.3. The date is created with fixed numbers, so the output is the same on every run (the format uses the browser locale, en-US by default).

Key points

  • `value | pipe:arg` transforms for display only, and pipes can be chained.
  • Custom pipe: `@Pipe({ name })` plus `transform()`. Add it to `imports`.
  • Pure pipes run only when inputs change. Impure pipes run on every check.
  • Prefer `computed` for filtering or sorting data, and pipes for formatting.

Common mistakes

  • Making a filtering pipe that receives a mutated array. It never re-runs because the reference is the same.
  • Marking a pipe `pure: false` to fix a stale view, which hides the real bug and slows the app.
  • Forgetting to add the pipe (including `DatePipe`, `CurrencyPipe`) to `imports`.

Follow-ups

  • Why is there no built-in filter pipe in Angular?
  • What does the `async` pipe do and why is it impure?
  • How would you test a pipe?

Tips

  • Show that you know pure versus impure. It is the usual follow-up.
  • Say you format dates and numbers with pipes so they follow the user's locale.
  • Keep the custom pipe tiny and testable as a plain function.

Evaluation rubric

  • Writes a working custom pipe with PipeTransform.
  • Explains pure and impure behaviour.
  • Imports built-in pipes explicitly.