Chapter 4.3โ˜• 15 min read

Built-in Pipes

Pipes transform data in templates โ€” formatting dates, currencies, and more.

01What is a Pipe

Pipes transform data in the template before displaying it to the user.

"Pipe = filter โ€” data ko pipe se guzaro, clean formatted data niklo." Just like a water filter transforms dirty water to clean water, a pipe transforms raw data to formatted data.

Syntax: {{ value | pipeName }}

You can chain pipes: {{ value | pipe1 | pipe2 }} โ€” output of pipe1 becomes input of pipe2.

Angular comes with many built-in pipes. Let's explore the most useful ones.

02DatePipe โ€” Format Dates

DatePipe formats JavaScript Date objects:

{{ today | date }} โ†’ Jan 15, 2025\n{{ today | date:'shortDate' }} โ†’ 1/15/25\n{{ today | date:'mediumDate' }} โ†’ Jan 15, 2025\n{{ today | date:'fullDate' }} โ†’ Wednesday, January 15, 2025\n{{ today | date:'shortTime' }} โ†’ 10:30 AM\n{{ today | date:'dd/MM/yyyy' }} โ†’ 15/01/2025\n{{ today | date:'dd/MM/yyyy HH:mm' }} โ†’ 15/01/2025 10:30

Custom format codes: dd=day, MM=month, yyyy=year, HH=hours, mm=minutes.

"DatePipe = calendar โ€” kaise bhi date dikhana ho, format do."

Important: DatePipe shows dates in the browser's local timezone by default.

03CurrencyPipe, NumberPipe, PercentPipe

CurrencyPipe formats numbers as currency:

{{ 500 | currency:'INR' }} โ†’ โ‚น500.00\n{{ 500 | currency:'INR':'symbol-narrow' }} โ†’ โ‚น500.00\n{{ 500 | currency:'USD':'symbol' }} โ†’ $500.00\n{{ 500 | currency:'EUR':'code' }} โ†’ EUR 500.00

DecimalPipe (NumberPipe):

{{ 100000 | number }} โ†’ 1,00,000 (Indian formatting!)\n{{ 3.14159 | number:'1.2-3' }} โ†’ 3.142\n// Format: minIntegerDigits.minFractionDigits-maxFractionDigits

PercentPipe:

{{ 0.2 | percent }} โ†’ 20%\n{{ 0.1234 | percent:'1.2-2' }} โ†’ 12.34%

"NumberPipe = Indian counting โ€” lakh, crore format free mein."

04UpperCasePipe, LowerCasePipe, TitleCasePipe

UpperCasePipe, LowerCasePipe, TitleCasePipe โ€” simple string transformations:

{{ 'hyderabad' | uppercase }} โ†’ HYDERABAD\n{{ 'ANGULAR' | lowercase }} โ†’ angular\n{{ 'hYDERABAD bIRYANI' | titlecase }} โ†’ Hyderabad Biryani

SlicePipe โ€” extract a portion of an array or string:

{{ 'Hello World' | slice:0:5 }} โ†’ Hello\n{{ items | slice:0:3 }} โ†’ first 3 items

JsonPipe โ€” convert object to JSON string (useful for debugging):

{{ user | json }} โ†’ { "name": "Raj", "age": 25 }

KeyValuePipe โ€” iterate over object properties:

@for (item of obj | keyvalue; track item.key) {\n  {{ item.key }}: {{ item.value }}\n}
05AsyncPipe โ€” Unsubscribe Automatically

AsyncPipe is the most powerful pipe โ€” it subscribes to an Observable or Promise automatically and unsubscribes on component destroy:

// Component: order$ = this.http.get('/api/order');\n// Template:\n{{ order$ | async }}  โ†’  Subscribes, shows data, auto-unsubscribes!

"AsyncPipe = automatic maid โ€” khud subscribe karo, khud clean karo."

Benefits:

  • โœ… Automatically subscribes when component loads
  • โœ… Automatically unsubscribes when component destroys (no memory leaks!)
  • โœ… Works with Observable, Promise, and Signal

Common pattern with @if:

@if (data$ | async; as data) {\n  <p>{{ data.name }}</p>\n}

This subscribes once, assigns the value to the data variable, and shows it.

Key Takeaways

  • โœ… Pipes transform data in templates โ€” {{ value | pipeName }}.
  • โœ… DatePipe formats dates, CurrencyPipe formats money, NumberPipe formats numbers.
  • โœ… UpperCasePipe, LowerCasePipe, TitleCasePipe transform strings.
  • โœ… SlicePipe extracts array/string portions; JsonPipe helps debug.
  • โœ… AsyncPipe auto-subscribes and auto-unsubscribes โ€” no memory leaks!
Course Search
Search across all chapters & stages
๐Ÿ“–

Search the course

Type any topic โ€” branching, stash, rebase, hooks โ€” and jump straight to that chapter.

merge branchesgit stashundo commitrebase