Sub topic 13
Migrating from *ngIf, *ngFor, and ngSwitch
This chapter assumes you know Angular components and templates, basic TypeScript, and the built-in blocks @if, @for, and @switch. You will convert a template by hand, then use the Angular CLI schematic to do it across a project.
What changes and what stays
The old structural directives (*ngIf, *ngFor, ngSwitch) are directives from CommonModule. The built-in blocks are template syntax that the compiler understands directly. You do not import anything to use them.
| Old syntax | New syntax | Notable difference | Column | Column |
|---|---|---|---|---|
| `*ngIf="cond; else tpl"` | `@if (cond) { } @else { }` | No `ng-template` reference needed | ||
| `*ngIf="x \ | async as y"` | `@if (x \ | async; as y) { }` | Same alias idea, different position |
| `*ngFor="let i of items; trackBy: fn"` | `@for (i of items; track i.id) { }` | `track` is required and is an expression | ||
| `*ngFor` with empty check | `@for ... { } @empty { }` | Built in, no separate `*ngIf` | ||
| `[ngSwitch]` / `*ngSwitchCase` | `@switch` / `@case` | Strict equality, no fall-through |
A manual conversion
Start with a template that uses all the old pieces: a conditional with an else template, and a list with a trackBy function.
<!-- Before -->
<div *ngIf="items.length; else none">
<app-item *ngFor="let item of items; trackBy: trackById" [item]="item" />
</div>
<ng-template #none><p>No items.</p></ng-template>
<!-- After -->
@if (items.length) {
@for (item of items; track item.id) {
<app-item [item]="item" />
}
} @else {
<p>No items.</p>
}Four things happened:
- The
elsetemplate reference#noneand itsng-templatebecame an@elseblock. let item of itemsbecameitem of items. There is noletin the@forheader.- The
trackBy: trackByIdfunction becametrack item.id, so thetrackByIdmethod in the component class can be deleted if nothing else uses it. - The wrapper
divstayed here only because the original had one. Blocks do not add a host element, so you can drop wrappers that existed only to hold a directive.
If you want the empty message to belong to the list itself, @empty is shorter than the outer @if:
@for (item of items; track item.id) {
<app-item [item]="item" />
} @empty {
<p>No items.</p>
}Choosing the track expression
trackBy took a function with the signature (index, item). track takes an expression evaluated for each item. Pick the same stable identity you used before, usually an id. If your old trackBy returned index, write track $index. If you had no trackBy, Angular's old default compared by object identity, so track item is the closest equivalent. A stable id is still better when items are recreated from server data.
Context variables
The old index as i and even as isEven forms become aliases on $-prefixed variables:
<!-- Before -->
<li *ngFor="let t of tasks; index as i; even as isEven; trackBy: byId">{{ i }}</li>
<!-- After -->
@for (t of tasks; track t.id; let i = $index, isEven = $even) {
<li>{{ i }}</li>
}The available variables are $index, $count, $first, $last, $even, and $odd. Inside the block you can also use them without aliasing.
Switching
ngSwitchCase and @case both compare with strict equality. The default branch changes name:
<!-- Before -->
<div [ngSwitch]="status">
<p *ngSwitchCase="'loading'">Loading...</p>
<p *ngSwitchDefault>Data is ready.</p>
</div>
<!-- After -->
@switch (status) {
@case ('loading') { <p>Loading...</p> }
@default { <p>Data is ready.</p> }
}Automating with the schematic
For a whole project, run the Angular CLI schematic from the workspace root:
ng generate @angular/core:control-flowIt rewrites *ngIf, *ngFor, and ngSwitch usage in your templates. It also asks which path to migrate, so you can convert one feature folder at a time.
After migrating, remove NgIf, NgFor, NgSwitch, and related imports from components that no longer use them. Standalone components that still list them in imports compile, but the unused imports are dead weight.
Exercise
Convert this template to built-in control flow, keeping the same behavior: <li *ngFor="let u of users; trackBy: byId; let last = last">{{ u.name }}<span *ngIf="!last">,</span></li>, with a fallback message "No users" when the list is empty.
Show answer
@for (u of users; track u.id; let last = $last) {
<li>{{ u.name }}@if (!last) {<span>,</span>}</li>
} @empty {
<li>No users</li>
}
The byId function becomes track u.id, last is aliased from $last, and @empty replaces a separate emptiness check.