Angular's new template syntax: @if, @for and @defer

The new block syntax came out as a developer preview in Angular 17. With Angular 18, @if, @for, @switch and @defer are all stable.

The new block syntax came out as a developer preview in Angular 17. With Angular 18, @if, @for, @switch and @defer are all stable. It looks like a cosmetic change at first, but @for and @defer in particular change how you write templates.

@if

@if (user(); as u) {
  <h1>Welcome back, {{ u.name }}</h1>
} @else if (loading()) {
  <p>Loading...</p>
} @else {
  <a href="/login">Sign in</a>
}

Compare that with *ngIf=“user; else loadingOrLogin” and two s further down. The else if alone makes it worth switching. It’s also built into the compiler, so there’s nothing to import. No more forgetting NgIf in a standalone component.

@for and track

<ul>
  @for (order of orders(); track order.id; let i = $index, last = $last) {
    <li [class.last]="last">{{ i + 1 }}. {{ order.id }}: {{ order.total }}</li>
  } @empty {
    <li>No orders yet.</li>
  }
</ul>

track is required. Leave it out and the build fails:

error NG5002: @for loop must have a "track" expression

That’s a good thing. With *ngFor, trackBy was optional, so most lists didn’t have one, and Angular re-created DOM nodes whenever the array was replaced, for example after every HTTP refresh. Now you have to decide. Track by a stable id when you have one. track $index is fine for static lists that never reorder.

The @empty block replaces the usual *ngIf=“orders.length === 0” next to the loop. $index, $first, $last, $even, $odd and $count are all available.

@switch

@switch (user()?.role) {
  @case ('admin') { <button>Manage users</button> }
  @case ('editor') { <button>New post</button> }
  @default { <span>Read only</span> }
}

It’s the same as ngSwitch, just easier to read. Cases don’t fall through.

@defer

This is the genuinely new part. @defer lazy loads a piece of a template, along with the code for the components inside it, without touching the router.

@defer (on viewport) {
  <app-sales-chart />
} @placeholder {
  <div class="chart-placeholder"></div>
} @loading (after 100ms; minimum 500ms) {
  <p>Loading chart...</p>
} @error {
  <p>Couldn't load the chart.</p>
}

The chart component, and the charting library it imports, end up in their own chunk. That chunk is only downloaded when the placeholder scrolls into view. For a dashboard with a heavy chart below the fold, this takes a real bite out of the initial bundle.

The sub-blocks:

  • @placeholder shows before loading starts. With on viewport it needs a single root element, since that’s what Angular watches.
  • @loading shows while the chunk downloads. after 100ms avoids a flash on fast connections, and minimum 500ms avoids one that disappears too quickly to read.
  • @error shows if the chunk fails to load.

Triggers I use most:

<!-- load when the user clicks the placeholder, but prefetch when the browser is idle -->
@defer (on interaction; prefetch on idle) {
  <app-comments [postId]="post.id" />
} @placeholder {
  <button>Show comments</button>
}

<!-- load when a condition becomes true -->
@defer (when showAdvanced()) {
  <app-advanced-filters />
}

Other triggers are on idle (the default), on immediate, on hover and on timer(2s).

Why your @defer might not split anything

The deferred components have to be standalone, and they must not be used anywhere else in the same component, not even in its imports for another part of the template. If they are, Angular can’t split them out and loads them eagerly. The build still works, so it’s easy to miss. Check the build output for a separate lazy chunk to be sure.

Also, @defer is a client-side feature. During server-side rendering, only the @placeholder is rendered.

Migrating

There’s a schematic that converts *ngIf, *ngFor and ngSwitch in your templates:

ng generate @angular/core:control-flow

It carries trackBy functions over into track expressions. For loops without one, check what it generated. Then remove NgIf, NgFor and NgSwitch from your component imports, since they’re no longer used. The old directives still work, so you can migrate one folder at a time.