Angular · Migration · Frontend · Performance
Migrating to Angular's Built-in Control Flow: *ngIf and *ngFor to @if and @for
A practical guide to moving from *ngIf, *ngFor and ngSwitch to Angular's @if, @for and @switch blocks: the schematic, track expressions, @empty and the edge cases.

When I took a large Angular codebase from version 7 to 20, the built-in control flow was one of the most satisfying steps. Templates got shorter, CommonModule imports disappeared from standalone components and list rendering got faster because track forced us to think about identity. It was also the step where a schematic did 90% of the work and the last 10% needed a careful human. This post covers both parts.
What changes and why it matters
Structural directives (*ngIf, *ngFor, [ngSwitch]) are regular directives imported from CommonModule. The new blocks (@if, @for, @switch, @defer) are part of the template syntax itself, so the compiler understands them directly.
| Old | New | Notes |
|---|---|---|
*ngIf="cond; else other" | @if (cond) { } @else { } | @else if is now possible |
*ngIf="obs$ | async as value" | @if (obs$ | async; as value) { } | Note the semicolon |
*ngFor="let x of xs; trackBy: fn" | @for (x of xs; track x.id) { } | track is mandatory |
[ngSwitch] + *ngSwitchCase | @switch (v) { @case (1) { } } | No fall-through |
Empty-list *ngIf | @empty { } | Inside @for |
The practical wins: no directive imports, better type narrowing inside @if, and a @for implementation that is noticeably cheaper at diffing large lists.
Step 1: run the schematic
Angular ships a migration. Run it on a clean working tree so the diff is reviewable on its own:
ng generate @angular/core:control-flow
It asks for a path, so you can migrate one feature folder at a time. In a big repo I did exactly that: one folder per pull request, each one small enough to review. After it runs, also remove imports that are now unused:
ng generate @angular/core:cleanup-unused-imports
If your Angular version doesn't have that second schematic yet, your linter will flag the unused NgIf, NgFor and CommonModule imports instead.
Step 2: fix the track expressions
The schematic converts trackBy: trackById into track trackById($index, item), and when there was no trackBy at all it writes track $index. Neither is ideal. track $index is legal but means "identity is position", so inserting an item at the top re-renders every row. Replace it with a real key:
@for (device of devices(); track device.id) {
<app-device-row [device]="device" />
} @empty {
<p class="muted">No devices connected.</p>
}
Use $index only for static lists that never reorder. If items have no stable ID, give them one when the data arrives instead of tracking by object reference, which breaks the moment you replace the array with fresh API data.
@for also gives you $index, $first, $last, $even, $odd and $count without aliasing:
@for (step of steps; track step.key) {
<li [class.done]="$index < currentStep">{{ $index + 1 }}. {{ step.label }}</li>
}
Step 3: the edge cases the schematic can't decide
ng-template references with else. If an else template is reused in several places, the schematic keeps the ng-template and uses ngTemplateOutlet. That works, but it is usually clearer to inline the block or move it into a small component.
as aliases with async. *ngIf="user$ | async as user" hides the falsy case. With @if (user$ | async; as user) the behaviour is the same, so 0, '' or false still render nothing. If a value can legitimately be zero, compare explicitly: @if ((count$ | async) !== null).
Switch fall-through. ngSwitch never fell through either, but some templates used several *ngSwitchCase elements with the same value to show two blocks. In @switch, only one @case matches, so merge them.
Content projection. When a component uses <ng-content select="...">, content inside an @if or @for block is matched by the block's single root element. If a block has several root nodes, they fall back to the default slot. Check components with named slots, ideally with visual tests.
Step 4: verify behaviour, not syntax
A template migration rarely breaks compilation; it breaks behaviour in subtle ways. What helped me:
- Run the component tests after each folder. Tests that query the DOM catch missing
@elsebranches quickly; see Angular component testing. - Take screenshots of key screens before and after. Empty states and loading states are the most common regressions.
- Watch list-heavy screens in the profiler. With proper
trackkeys andOnPush, re-renders drop clearly; the details are in Angular OnPush performance.
Where @defer fits
Once you are on the new syntax, @defer is the next tool: it lazy-loads a block's components when it enters the viewport, on interaction or when the browser is idle.
@defer (on viewport) {
<app-run-history-chart [runId]="runId" />
} @placeholder {
<div class="chart-skeleton"></div>
}
It is a cheap way to take heavy charts or editors out of the initial bundle without touching routing. Keep the placeholder the same size as the content, otherwise you trade a smaller bundle for layout shift.
A rollout order that works
- Upgrade to a version with stable control flow (17+) and finish the standalone migration where you can; both migrations touch the same imports.
- Migrate shared UI components first, then feature folders.
- Replace every
track $indexthat sits on dynamic data. - Remove unused
CommonModuleimports. - Add
@deferonly where a bundle analysis shows a real win.
Conclusion
The control flow migration is mostly mechanical, which is exactly why it is easy to rush. Let the schematic do the conversion, then spend your attention on track keys, falsy async values and empty states. Done folder by folder, it is a low-risk change that leaves templates cleaner and lists faster. For the wider upgrade path, see the Angular 7 to 20 upgrade guide.


