DEV Community

Cover image for Angular Pipes Explained — From Basics to Custom Pipes (With Real Examples)
ROHIT SINGH
ROHIT SINGH

Posted on • Edited on

Angular Pipes Explained — From Basics to Custom Pipes (With Real Examples)

Angular pipes look simple — but most developers either underuse them or misuse them.

If you’ve ever written formatting logic inside your component just to display data in the UI, this blog is for you.

Let’s break Angular Pipes clearly, practically, and correctly.

🔹 What Are Angular Pipes?

Angular Pipes are used to transform data in templates without changing the original value.

They are:

Declarative

Reusable

Optimized for UI formatting

Simple example:

<p>{{ price | currency }}</p>
Enter fullscreen mode Exit fullscreen mode

👉 The price value stays the same in your component
👉 Only the view output is transformed

🔹 Why Pipes Exist (Real Problem)

Without pipes, templates become messy:

<p>{{ price.toFixed(2) }}</p>
<p>{{ name.toUpperCase() }}</p>
Enter fullscreen mode Exit fullscreen mode

Now imagine this logic repeated in 10 components 😵‍💫

Pipes solve this by:

Centralizing logic

Keeping templates clean

Improving readability

🔹 Built-in Angular Pipes (Most Used)
1️⃣ Date Pipe

<p>{{ today | date:'dd MMM yyyy' }}</p>
Enter fullscreen mode Exit fullscreen mode

Output:

09 Jan 2026

2️⃣ Currency Pipe

<p>{{ amount | currency:'INR' }}</p>
Enter fullscreen mode Exit fullscreen mode

Output:

₹1,250.00
Enter fullscreen mode Exit fullscreen mode

3️⃣ UpperCase / LowerCase

<p>{{ username | uppercase }}</p>
Enter fullscreen mode Exit fullscreen mode

4️⃣ Decimal Pipe

<p>{{ rating | number:'1.1-2' }}</p>
Enter fullscreen mode Exit fullscreen mode

5️⃣ Slice Pipe

<p>{{ title | slice:0:10 }}</p>
Enter fullscreen mode Exit fullscreen mode

🔹 Chaining Pipes (Very Powerful)

Angular allows multiple pipes together:

<p>{{ name | uppercase | slice:0:5 }}</p>
Enter fullscreen mode Exit fullscreen mode

Output:

ANSHU
Enter fullscreen mode Exit fullscreen mode

Readable + clean + expressive.

🔹 Creating a Custom Pipe (Real Use Case)
🧠 Scenario

You want to display “Active” / “Inactive” instead of true / false.

Step 1: Generate Pipe
ng generate pipe status

Step 2: Pipe Implementation

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

@Pipe({
  name: 'status'
})
export class StatusPipe implements PipeTransform {
  transform(value: boolean): string {
    return value ? 'Active' : 'Inactive';
  }
}
Enter fullscreen mode Exit fullscreen mode

Step 3: Use in Template

<p>{{ user.isActive | status }}</p>
Enter fullscreen mode Exit fullscreen mode

Output:

Active
Enter fullscreen mode Exit fullscreen mode

🔹 Pure vs Impure Pipes (IMPORTANT)
✅ Pure Pipes (Default)

Executes only when input changes

Very fast

Best for formatting

@Pipe({
  name: 'status',
  pure: true
})
Enter fullscreen mode Exit fullscreen mode

Angular assumes the pipe is pure by default.

⚠️ Impure Pipes

Runs on every change detection cycle.

@Pipe({
  name: 'filterData',
  pure: false
})
Enter fullscreen mode Exit fullscreen mode

❌ Can kill performance
✔ Use only when absolutely required

🔹 Pipe with Arguments

Example: Capitalize first letter

transform(value: string, limit: number): string {
  return value.slice(0, limit).toUpperCase() + value.slice(limit);
}
Enter fullscreen mode Exit fullscreen mode

Usage:

<p>{{ name | customPipe:1 }}</p>
Enter fullscreen mode Exit fullscreen mode

🔹 Pipes vs Methods in Template

❌ Bad Practice:

<p>{{ getFormattedDate() }}</p>
Enter fullscreen mode Exit fullscreen mode

Why?

Method runs on every change detection

Performance hit

✅ Better:

<p>{{ date | date }}</p>
Enter fullscreen mode Exit fullscreen mode

🔹 When NOT to Use Pipes

Avoid pipes when:

Business logic is complex

Data transformation affects application state

Heavy computations are involved

Pipes are for UI-level transformations only.

🔹 Performance Tip (Senior-Level)

Pipes are faster than methods in templates — but impure pipes are slower than both.

Always ask:

Can this be pure?

Can this be done once in component?

🔥 Real-World Use Cases

✔ Formatting prices
✔ Showing readable dates
✔ Displaying status labels
✔ UI-only transformations
✔ Cleaner HTML templates

✅ Final Takeaway

Angular Pipes are:

Simple on the surface

Powerful when used correctly

Dangerous when misused

Use pipes for presentation, not logic.

Master this, and your Angular code instantly looks cleaner and more professional.

Top comments (0)