Built-in Pipes
Pipes transform data in templates โ formatting dates, currencies, and more.
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.
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.
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."
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}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!
Want to track your progress?
Log in to save your place and pick up where you left off.
Progress track karna chahte ho?
Login karo apni progress save karne ke liye aur jahan chhoda tha wahan se shuru karo.
Login