Sub topic 1
Why Angular has built-in control flow
Angular templates often need to show or hide content, repeat it for each item in a list, or pick one of several versions. For years, Angular did this with structural directives such as *ngIf, *ngFor, and ngSwitch. Since Angular 17, the template syntax itself has built-in control flow blocks: @if, @for, and @switch. This chapter explains what changed and why. Later chapters cover each block in detail.
Prerequisites
This chapter assumes you already know:
- Basic TypeScript syntax.
- How Angular components and templates fit together.
- Interpolation (
{{ value }}) and event or property binding. - Signals or observables. This is helpful but not required.
The problem with structural directives
A structural directive is a class that changes the DOM structure. The * prefix is shorthand: Angular rewrites *ngIf="cond" into an <ng-template> wrapped around the element, and the directive decides at runtime whether to render it.
This works, but it has costs:
- Imports.
NgIf,NgFor, andNgSwitchmust be imported into every standalone component or module that uses them. Forgetting one produces a template error. - Awkward alternatives. An
elsebranch needs a separate<ng-template>with a template reference variable. - Optional performance hints.
trackByis optional, so many lists re-create DOM nodes when the data changes. - Extra wrapper elements.
ngSwitchneeds a host element, and a*ngIfand*ngForon the same element is not allowed.
What built-in control flow changes
Built-in control flow is part of the template language. The compiler understands @if, @for, and @switch directly, so no directive class is involved at runtime. Here is a first look at @if with @else:
import { Component, signal } from '@angular/core';
@Component({
selector: 'app-session',
template: `
@if (isLoggedIn()) {
<p>Welcome back!</p>
<button (click)="isLoggedIn.set(false)">Log out</button>
} @else {
<button (click)="isLoggedIn.set(true)">Log in</button>
}
`,
})
export class SessionComponent {
isLoggedIn = signal(false);
}The component has no imports array, yet the template works. The @else branch sits directly after the @if block, with no <ng-template> and no reference variable.
The same template, before and after
The two styles can express identical logic. Compare them:
<!-- 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>
}Three differences show up here:
- The
elsebranch is written inline, next to the condition it belongs to. track item.idreplaces thetrackByfunction. It is a plain expression, and it is required.- The wrapper
<div>is gone, because blocks add no host element.
The three blocks at a glance
| Block | Replaces | Purpose |
|---|---|---|
| `@if`, `@else if`, `@else` | `*ngIf` with `else` templates | Render content based on a condition |
| `@for`, `@empty` | `*ngFor` | Repeat content for each item, with a fallback for empty collections |
| `@switch`, `@case`, `@default` | `ngSwitch`, `*ngSwitchCase`, `*ngSwitchDefault` | Pick one template by comparing a value |
Why this matters
- Less boilerplate. There are no directive imports and no
<ng-template>plumbing. - Safer lists. Requiring
trackpushes you to choose how items are identified, which affects which DOM nodes Angular reuses. - Better type checking. The compiler knows the block structure, so it can narrow types inside branches.
- Readable templates. The syntax resembles ordinary TypeScript control flow.
Exercise
Name two things that a template using *ngIf with an else branch needs, which the equivalent @if with @else does not.
Show answer
It needs NgIf imported into the component (or its module), and it needs a separate <ng-template> with a template reference variable (such as #none) for the else content. With @if, neither is required: the @else block is written directly after the @if block.