Sitemap
The Frontend Fuel

The Frontend Fuel

Exploring JavaScript, web development, and modern frameworks. Sharing insights, best practices, and real world coding challenges. Let's code smarter 🚀

Angular Pipe

--

Press enter or click to view image in full size

A pipe in Angular is a feature that allows you to transform data in the template. It is typically used to format data before displaying it to the user directly in the HTML view.

Get The Frontend Fuel’s stories in your inbox

Join Medium for free to get updates from this writer.

A pipe is a filter or formatter that takes raw data and outputs something more readable or desirable.

Why Use Pipes?

  • Reusability (a pipe can be reused across components).
  • Separation of concerns (logic stays in the pipe, not in the component).
  • Improves readability and maintainability.
  • Cleaner templates (you don’t clutter HTML with logic)

Custom Pipe Example (Pure Pipe)

Let’s say you want to convert a string to uppercase but add a suffix:

import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
name: 'appendText' // Use this name in HTML
})
export class AppendTextPipe implements PipeTransform {
transform(value: string, suffix: string): string {
return value.toUpperCase() + suffix;
}
}

In HTML

<p>{{ 'angular' | appendText:'🔥' }}</p>

Impure Pipe Example

Imagine you have a list that can change without reference change (like push/pop on an array). You might need an impure pipe to filter that list dynamically.

@Pipe({
name: 'filterItems',
pure: false
})
export class FilterItemsPipe implements PipeTransform {
transform(items: any[], term: string): any[] {
return items.filter(item => item.name.includes(term));
}
}
<li *ngFor="let item of items | filterItems:searchTerm">{{ item.name }}</li>

Impure Pipe

  • Re-evaluated on every change detection cycle.
  • It can impact performance.
  • Useful for things like filtering mutable arrays or formatting dynamic data like timestamps.

Only one argument

If your pipe needs only one argument (e.g., to find the square root of a number), here’s exactly how it works in Angular.

import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
name: 'sqrt'
})
export class SqrtPipe implements PipeTransform {
transform(value: number): number {
return Math.sqrt(value);
}
}


<p>{{ 49 | sqrt }}</p>
  • If a pipe takes only one argument, that argument is the value before the | symbol.
  • You don’t need extra parameters in the template or transform method.

Angular’s Flow:

  • Angular gets items from your component.
  • It passes items and searchTerm to transform().
  • transform() returns a filtered array.
  • *ngFor loops over the filtered array, assigning each element to item.

Visual Flow

Component → [items] + [searchTerm]
↓
Pipe: filterItems.transform(items, searchTerm)
↓
Filtered Array → used in *ngFor="let item of [filtered items]"

Conclusion for Pipes in Angular (Short & Clear)

  • Pipes are used to transform data directly in the template (e.g., format, filter, append).
  • Angular provides built-in pipes like date, uppercase, currency, etc.
  • You can create custom pipes for reusable transformation logic.
  • Pure pipes (default) run only when input/reference changes — good for performance.
  • Impure pipes run on every change detection, which is useful for mutable data like arrays.
  • Syntax:
{{ value | pipeName:arg1:arg2 }}

// Internally, it calls:
transform(value, arg1, arg2)
import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
name: 'sqrt'
})
export class SqrtPipe implements PipeTransform {
transform(value: number): number {
return Math.sqrt(value);
}
}

👉 Use pipes to keep your templates clean, readable, and efficient.

--

--

The Frontend Fuel
The Frontend Fuel

Written by The Frontend Fuel

Exploring JavaScript, web development, and modern frameworks. Sharing insights, best practices, and real world coding challenges. Let's code smarter 🚀