AI Hints
Home›Angular Tutorial›Custom Pipes, Directives and Custom Attribute Directives
Angular · Lesson 58 of 120

Custom Pipes

Your own formatting.

⏱ 2 min read▶ Video at 1:08:14🎓 Free
Watch this lesson in the Angular courseWatch on YouTube ↗Full Angular course

Our shop shows stock as a number. But customers want words, like sold out, or only two left. No built-in pipe does that, so we'll write our own.

  • Stock as a number
  • Customers want words
  • Write a pipe

Writing a pipe

A pipe is a class, with the pipe decorator. The decorator gives it the name used in templates. The class has one method, transform. It receives the value, and returns what to show.

keyvalue
@Pipe({ name: '...' })the name in templates
class ... implements PipeTransformthe pipe class
transform(value)receives the value
returnwhat to show

stock-label-pipe.ts

The pipe decorator gives the pipe its name, stock label. Transform receives the value before the bar, and returns the text to show. Zero becomes sold out, and fewer than three becomes only a few left.

stock-label-pipe.tstypescript
import { Pipe, PipeTransform } from '@angular/core';

@Pipe({ name: 'stockLabel' })
export class StockLabelPipe implements PipeTransform {
  transform(stock: number): string {
    if (stock === 0) return 'Sold out';
    if (stock < 3) return `Only ${stock} left`;
    return 'In stock';
  }
}

Using the pipe

We import the pipe class, like any other. Then each book's stock goes through stock label. Run it. Each book shows a label, instead of a number.

app.tstypescript
import { Component } from '@angular/core';
import { StockLabelPipe } from './stock-label-pipe';
import { BOOKS } from './books';

@Component({
  selector: 'app-root',
  imports: [StockLabelPipe],
  template: `
    @for (b of books; track b.id) {
      <p>{{ b.title }}: {{ b.stock | stockLabel }}</p>
    }
  `,
})
export class App {
  books = BOOKS;
}
Also uses stock-label-pipe.ts
import { Pipe, PipeTransform } from '@angular/core';

@Pipe({ name: 'stockLabel' })
export class StockLabelPipe implements PipeTransform {
  transform(stock: number): string {
    if (stock === 0) return 'Sold out';
    if (stock < 3) return `Only ${stock} left`;
    return 'In stock';
  }
}
Also uses books.ts
export interface Book {
  id: number;
  title: string;
  author: string;
  price: number;
  stock: number;
}

export const BOOKS: Book[] = [
  { id: 1, title: 'Dune', author: 'Frank Herbert', price: 9.99, stock: 4 },
  { id: 2, title: 'The Hobbit', author: 'J.R.R. Tolkien', price: 8.99, stock: 0 },
  { id: 3, title: 'Circe', author: 'Madeline Miller', price: 10.99, stock: 2 },
  { id: 4, title: 'Emma', author: 'Jane Austen', price: 6.99, stock: 7 },
  { id: 5, title: 'Persuasion', author: 'Jane Austen', price: 7.49, stock: 1 },
];

Pure pipes

By default, a pipe is pure. Angular only runs it again when its input value changes. That makes pipes very cheap, even in long lists. So keep transform simple, with no side effects.

keyvalue
Purethe default
Runs againonly when the input changes
Cheapeven in long lists
transformsimple, no side effects

Summary

A custom pipe is a class with a name, and a transform method. Next, directives.

Key takeaways
  • @Pipe({ name })
  • transform(value)
  • Pure by default
Next up: Directives — add behaviour to elements