Control Flow: @if, @for, @switch and @defer

Angular 15 min min read Updated: Nov 16, 2026 Beginner
Control Flow: @if, @for, @switch and @defer
Beginner Topic 3 of 7

The New Template Syntax

Angular has built-in control flow blocks that start with @. They replaced the older *ngIf, *ngFor and *ngSwitch directives. They are faster, need no imports and are easier to read. Old directives still work in existing code.

@if, @else if, @else

html @if (user) { <p>Welcome back, {{ user.name }}!</p> } @else if (loading) { <p>Loading...</p> } @else { <p>Please log in.</p> } <!-- save the result with 'as' --> @if (user(); as u) { <h2>{{ u.name }}</h2> }

@for with track

typescript export class TodoList { todos = [ { id: 1, title: 'Learn Angular', done: false }, { id: 2, title: 'Build app', done: false }, ]; }
html <ul> @for (todo of todos; track todo.id) { <li [class.done]="todo.done"> {{ $index + 1 }}. {{ todo.title }} </li> } @empty { <li>No todos yet.</li> } </ul>
track is required: track todo.id tells Angular which item is which, so it only updates the items that changed when the list changes. Use a unique id. Use track $index only for simple static lists.
Implicit variableValue
$indexPosition (0-based)
$countNumber of items
$first, $lastTrue for first/last
$even, $oddTrue for even/odd positions
html @for (item of items; track item.id; let i = $index, last = $last) { <span>{{ i }}: {{ item.name }}@if (!last) {, }</span> }

@switch

html @switch (order.status) { @case ('new') { <span class="badge new">New</span> } @case ('shipped') { <span class="badge shipped">Shipped</span> } @case ('delivered') { <span class="badge ok">Delivered</span> } @default { <span>Unknown</span> } }

@let: Template Variables

html @let total = price * quantity; @let name = user()?.name ?? 'Guest'; <p>{{ name }} pays {{ total }}</p>

@defer: Lazy Load Parts of the Page

@defer loads a heavy component only when needed, which makes the first page load faster.

html @defer (on viewport) { <app-comments [postId]="id" /> } @placeholder { <p>Comments will appear when you scroll here</p> } @loading (minimum 300ms) { <p>Loading comments...</p> } @error { <p>Could not load comments.</p> }
TriggerLoads when
on viewportPlaceholder scrolls into view
on idleBrowser is idle (default)
on interactionUser clicks or presses the placeholder
on hoverMouse moves over it
on timer(3s)After a time
when conditionAn expression becomes true
html @defer (on interaction; prefetch on idle) { <app-chart [data]="data()" /> } @placeholder { <button>Show chart</button> }

Migrating Old Code

bash g generate @angular/core:control-flow

This schematic converts *ngIf, *ngFor and *ngSwitch to the new syntax automatically.

Conclusion

Use @if, @for (always with track), @switch and @defer for clean, fast templates. They need no imports, and the CLI can migrate old code.

Quick Summary: @if/@else, @for track + @empty, @switch, @let, @defer with triggers.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators