Skip to content
beginner Phase 6 · Pipes and Component Styling

Built-in Pipes

Format dates, numbers, currencies, and async values in templates.

30m
0 problems
Topic Progress 0%

Formatting Pipes: Date, Number, Currency

Pipes transform values for display without changing the underlying data. Angular provides several built-in pipes for common formatting needs.

Date Pipe

@Component({
  selector: 'app-event',
  template: `
    <p>Short: {{ eventDate | date: 'shortDate' }}</p>
    <p>Medium: {{ eventDate | date: 'mediumDate' }}</p>
    <p>Long: {{ eventDate | date: 'longDate' }}</p>
    <p>Full: {{ eventDate | date: 'fullDate' }}</p>
    <p>Custom: {{ eventDate | date: 'MMMM y, d' }}</p>
    <p>Time: {{ eventDate | date: 'shortTime' }}</p>
  `,
})
export class EventComponent {
  eventDate = new Date(2026, 8, 15, 14, 30);
}

Output:

  • Short: 9/15/26
  • Medium: Sep 15, 2026
  • Long: September 15, 2026
  • Full: Tuesday, September 15, 2026
  • Custom: September 2026, 15
  • Time: 2:30 PM

Currency Pipe

@Component({
  selector: 'app-price',
  template: `
    <p>{{ price | currency: 'USD' }}</p>
    <p>{{ price | currency: 'EUR': 'symbol' }}</p>
    <p>{{ price | currency: 'GBP': 'code' }}</p>
    <p>{{ price | currency: 'JPY': 'symbol': '1.0-0' }}</p>
  `,
})
export class PriceComponent {
  price = 1234.56;
}

Output:

  • $1,234.56
  • €1,234.56
  • GBP 1,234.56
  • ¥1,235

Decimal and Percent Pipes

@Component({
  selector: 'app-stats',
  template: `
    <p>Decimal: {{ value | number: '1.2-2' }}</p>
    <p>Percent: {{ ratio | percent: '1.0-1' }}</p>
  `,
})
export class StatsComponent {
  value = 3.14159;
  ratio = 0.8567;
}

Output:

  • Decimal: 3.14
  • Percent: 85.7%

Chaining Pipes

<p>{{ price | currency: 'USD': 'symbol' | uppercase }}</p>

Pipes are applied left to right. The output of one pipe becomes the input of the next.

The async Pipe

The async pipe subscribes to an Observable or Promise and returns the latest emitted value. It automatically unsubscribes when the component is destroyed.

Basic Usage

@Component({
  selector: 'app-user-list',
  template: `
    <div *ngIf="users$ | async as users">
      <p *ngFor="let user of users">{{ user.name }}</p>
    </div>

    <div *ngIf="loading$ | async as loading">
      <p>Loading...</p>
    </div>
  `,
})
export class UserListComponent {
  users$ = this.userService.getUsers();
  loading$ = this.userService.loading$;

  constructor(private userService: UserService) {}
}

Why Use async Pipe Instead of subscribe?

// BAD: Manual subscription — must unsubscribe
ngOnInit(): void {
  this.userService.getUsers().subscribe(users => {
    this.users = users;
  });
}

// GOOD: async pipe — auto-unsubscribe
users$ = this.userService.getUsers();

async Pipe with *ngIf as

The as syntax creates a local template variable from the emitted value:

<div *ngIf="user$ | async as user">
  <h2>{{ user.name }}</h2>
  <p>{{ user.email }}</p>
</div>

async Pipe with @if (New Control Flow)

@if (user$ | async; as user) {
  <h2>{{ user.name }}</h2>
} @else {
  <p>Loading...</p>
}

Combining async with Other Pipes

<p>{{ (user$ | async)?.name | uppercase }}</p>
<p>{{ (price$ | async) | currency: 'USD' }}</p>

Quiz

1. What does the async pipe do?

Question 1 options

2. How do you format a date as 'September 15, 2026'?

Question 2 options

3. What is the benefit of using async pipe over manual subscribe?

Question 3 options

Flashcards

Question

What does the date pipe do?

Answer

Formats a Date value using locale-aware patterns like 'shortDate', 'longDate', 'MMMM y'.

Question

What does the async pipe do?

Answer

Subscribes to an Observable/Promise in a template and auto-unsubscribes on component destroy.

Question

How do you chain pipes?

Answer

value | pipe1 | pipe2 — pipes apply left to right, each receiving the previous output.

Revision Notes

Key Takeaways

  • 1. Pipes transform values for display without changing the data
  • 2. DatePipe, CurrencyPipe, and NumberPipe handle common formatting
  • 3. The async pipe subscribes and auto-unsubscribes
  • 4. Pipes can be chained left to right

Interview Tips

  • • Explain why the async pipe prevents memory leaks
  • • Be ready to describe common date/currency formats
  • • Mention that pipes are pure by default (cached results)

Cheat Sheet

Built-in Pipes Cheat Sheet

Date:

{{ date | date: 'shortDate' }}
{{ date | date: 'longDate' }}
{{ date | date: 'MMMM y, d' }}

Currency:

{{ price | currency: 'USD' }}
{{ price | currency: 'EUR': 'symbol' }}

Number/Percent:

{{ value | number: '1.2-2' }}
{{ ratio | percent: '1.0-1' }}

Async:

<div *ngIf="users$ | async as users">

Continue Learning