Built-in control flow: @if, @for, and @switch
Angular's native control-flow blocks, and why they replaced the structural directives they look nothing like.
Interpolation and property, attribute, class, and style binding
Built-in control flow: @if, @for, and @switch
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- @if, @for, and @switch are parsed by the Angular compiler itself, not implemented as directives — that's why they no longer need the asterisk (`*`) syntax or a host element to attach to.
- @for requires a `track` expression — Angular refuses to compile a `@for` without one, because tracking by identity is what makes list updates fast instead of re-rendering everything.
- These blocks are a drop-in replacement for `*ngIf`/`*ngFor`/`*ngSwitch` at the template level; the underlying job — conditional and repeated rendering — hasn't changed, only how the compiler handles it.
Conditional rendering was always a directive pretending to be syntax
*ngIf="condition" reads like built-in template syntax, but it was never that —
it was a directive, and the asterisk was sugar hiding what that directive actually
did: wrap the element it sat on inside an <ng-template>, then conditionally
render that template based on the expression. You saw the real mechanism two
lessons ago — <ng-template> plus ngTemplateOutlet — because *ngIf was built
from exactly those pieces underneath its friendlier-looking syntax.
That worked, but “a directive secretly wrapping your element in a template” is an
indirect way to express something as fundamental as “show this, or don’t.”
Angular’s newer control-flow blocks — @if, @for, @switch — remove the
indirection entirely: they’re parsed by the template compiler itself, as actual
block syntax, not directives attached to an element.
The same jobs, a more direct syntax
@if (user) {
<h2>{{ user.name }}</h2>
} @else {
<p>No user signed in.</p>
}
@for (item of items; track item.id) {
<li>{{ item.name }}</li>
} @empty {
<li>No items yet.</li>
}
@switch (status) {
@case ('loading') { <spinner /> }
@case ('error') { <error-banner /> }
@default { <content /> }
}
Notice what’s gone: no asterisk, no <ng-container> needed just to group a few
elements under a condition, no separate *ngSwitchCase directive scattered across
sibling elements. @if/@else reads like an actual if/else statement. @switch
reads like an actual switch statement, with its cases nested inside the block
instead of spread across separate attributes. These aren’t new capabilities — a
conditional and a loop are the same two jobs *ngIf and *ngFor always did — but
the syntax finally matches how directly you think about the logic.
track: not optional, and not a style preference
@for has one requirement *ngFor never strictly enforced the same way:
you must supply a track expression. This isn’t a stylistic nicety — Angular’s
compiler refuses to build a @for block without one. The reason is about how list
re-renders actually work. When the list behind a @for changes, Angular needs to
answer “which DOM nodes correspond to which items now,” so it can move or update
existing elements instead of throwing everything away and rebuilding it from
scratch. track item.id tells Angular to match old and new items by that
identity. Without a stable identity to track by, the only remaining option is
matching by position — which silently breaks the moment a list is reordered,
filtered, or has an item removed from the middle, because position-based matching
has no way to tell “the fourth item changed” apart from “every item after the
third one shifted.”
This lesson’s video walks through all three blocks live, including the @empty
sub-block for @for’s empty-list case — worth watching closely since that
sub-block doesn’t have a direct *ngFor-era equivalent at all.
What’s next
The next lesson covers *ngIf/*ngFor/*ngSwitch properly — not because you
should reach for them in new code, but because you’ll meet them in essentially
every Angular codebase written before these blocks existed, and reading legacy
code fluently matters as much as writing new code well.
Check yourself
Why doesn't `@if` need an `<ng-container>` or a host element the way `*ngIf` effectively did?
`*ngIf` was a structural directive, and a directive needs something to attach to — hence the asterisk sugar that secretly wrapped your content in an `<ng-template>`. `@if` is a block the compiler understands directly; there's no directive instance being attached to anything, so there's no host-element requirement to route around.
What happens if you write `@for (item of items) { ... }` without a `track` expression?
It won't compile. Angular requires `track` on every `@for` specifically so it always knows how to match old list items to new ones by identity — without that, the only fallback would be comparing items by position, which silently produces wrong results the moment the list reorders.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.