Built-in and custom pipes

Intermediate · 7 min

Transforming data at the point it's displayed, and the pure/impure distinction that decides how often a pipe reruns.

Before thisInterpolation and property, attribute, class, and style binding

Code With Bubb · 7 min

Built-in and custom pipes

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Code With BubbReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • A pipe transforms a value right where it's displayed — `{{ price | currency }}` — instead of you pre-formatting it in the component class before the template ever sees it.
  • Pipes chain left to right: `{{ value | pipeA | pipeB }}` feeds `pipeA`'s output into `pipeB` as its input, reading in the same order you write it.
  • A pure pipe (the default) only reruns when its input reference changes; an impure pipe reruns on every change-detection cycle — a real performance difference, not a style choice.

Formatting belongs in the template, not scattered through the class

You could format a price in the component class before it ever reaches the template — this.formattedPrice = '$' + this.price.toFixed(2) — and that works, right up until you need the same formatting in three other components, or need to change currency per-locale, or just want your component class to hold data, not display logic. A pipe exists to pull that formatting out of the class entirely and express it declaratively, right at the point of display:

<p>{{ price | currency }}</p>

Read that as a sentence: take price, pipe it through currency. The component class never touches formatting at all — it holds a plain number, and the template decides how that number should look.

Built-in pipes, and the argument syntax

Angular ships a solid default set: date, currency, uppercase/lowercase, json (handy for debugging — dump any object’s structure straight into the template), percent, slice. Most accept arguments, passed after a colon:

{{ birthday | date:'fullDate' }}
{{ total | currency:'EUR':'symbol' }}
{{ items | slice:0:5 }}

And pipes chain — write {{ value | pipeA | pipeB }} and pipeA’s output becomes pipeB’s input, left to right, the same direction you read the expression. This lesson’s video builds and chains several pipes directly, which is worth watching closely for exactly this chaining behavior — it’s easy to assume pipes only work one at a time until you see several stacked together.

Writing your own

A custom pipe is a class with a transform method, marked with @Pipe:

@Pipe({ name: 'truncate', standalone: true })
export class TruncatePipe implements PipeTransform {
  transform(value: string, limit = 50): string {
    return value.length > limit ? value.slice(0, limit) + '…' : value;
  }
}
<p>{{ longText | truncate:100 }}</p>

transform takes the piped-in value as its first parameter, then whatever arguments were passed after the colons, and returns whatever the template should display instead. There’s genuinely nothing more to the mechanism than that — the built-in pipes above are written the exact same way, against the same PipeTransform interface.

Pure vs. impure: a performance distinction, not a style one

By default, every pipe is pure. A pure pipe’s transform only reruns when its input reference changes — not whenever change detection happens to run, and not when something inside the input object mutates without the reference itself changing. That’s exactly why mutating an array in place (items.push(x)) can silently fail to re-trigger a pipe watching that array: the reference never changed, so Angular has no reason to believe the output needs recomputing.

Marking a pipe pure: false flips that: it reruns on every change-detection cycle, regardless of whether its input reference changed. That sounds like it “just works” more reliably, and it does — at a real cost, because now that pipe’s logic runs far more often than a pure pipe’s would, on every check cycle whether anything relevant changed or not. The right fix for the mutated-array case above is almost always to stop mutating in place (replace the array with a new one when it changes) rather than reaching for pure: false as a workaround — keep impure pipes for the rare case where there’s genuinely no stable reference to key off of, like a pipe that has to inspect a Date object’s current real-world value on every tick.

Check yourself

  1. 01In `{{ total | currency:'EUR' }}`, what is `'EUR'` doing there?

    It's an argument to the `currency` pipe, passed after a colon — most built-in pipes accept one or more arguments this way to configure their output, here telling the pipe which currency code to format `total` as rather than using the default.

  2. 02You write a custom pipe that filters an array, and the list only updates after you replace the whole array — never when you push a new item into the existing array. Why?

    Your pipe is almost certainly pure (the default), and a pure pipe only reruns when the input's *reference* changes, not when the object it points to is mutated in place. Pushing into an existing array keeps the same reference, so Angular never calls the pipe again; replacing the array with a new one (`this.items = [...this.items, newItem]`) gives it a new reference and triggers a rerun.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark