Legacy structural directives: *ngIf, *ngFor, *ngSwitch

Intermediate · 8 min

How to read the asterisk-prefixed directive syntax you'll meet in any Angular codebase older than the control-flow blocks.

Before thisBuilt-in control flow: @if, @for, and @switch

Joshua Morony · 8 min

Legacy structural directives: *ngIf, *ngFor, *ngSwitch

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Joshua MoronyReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • The asterisk in `*ngIf` is shorthand the compiler expands into a full `<ng-template>` wrapping — you're writing sugar for machinery you've already seen directly.
  • `*ngFor`'s microsyntax (`let item of items; trackBy: trackFn`) packs several bindings into one attribute string, which is why it reads so differently from ordinary property binding.
  • `*ngSwitch` is three cooperating directives — `[ngSwitch]`, `*ngSwitchCase`, `*ngSwitchDefault` — not one, which is easy to miss since only the host element shows `[ngSwitch]`.

Reading the asterisk for what it actually expands to

You’ve already seen the real mechanism behind conditional rendering — <ng-template>, held inert until something renders it. *ngIf is that same idea, written as directive shorthand instead of spelled out:

<p *ngIf="user">{{ user.name }}</p>

<!-- what the asterisk actually desugars to -->
<ng-template [ngIf]="user">
  <p>{{ user.name }}</p>
</ng-template>

The asterisk isn’t special syntax of its own — it’s Angular telling you “this attribute is a structural directive, and I’m about to wrap your element in an <ng-template> and apply the directive to that template instead.” Once you’ve internalized that expansion, *ngIf stops being a thing to memorize and becomes an application of something you already understand: a directive that controls whether (and how many times) its attached template gets rendered.

*ngFor’s microsyntax, unpacked

<li *ngFor="let item of items; trackBy: trackByFn; index as i">
  {{ i }}: {{ item.name }}
</li>

That string after *ngFor= looks nothing like ordinary Angular binding syntax, because it isn’t — it’s a small, purpose-built microsyntax that Angular’s compiler parses into several separate bindings. let item of items sets up the loop variable and the collection to iterate. trackBy: trackByFn supplies the same identity-tracking job track does in @for, just with an actual function reference instead of an inline expression. index as i exposes the current index under a name you choose. Each of these pieces could in principle be a separate input on the directive — ngForOf, ngForTrackBy — and in fact that’s literally what they are under the hood; the microsyntax is just a more readable way to write several directive inputs in one attribute instead of as awkward [ngForOf], [ngForTrackBy] bindings spelled out individually.

trackBy matters here for exactly the reason track is mandatory in @for: without it, Angular can only match old and new list items by position, which breaks the moment the list reorders or an item is removed from the middle. The linked “deeper” video for this lesson is dedicated entirely to that performance angle — worth watching once you’ve seen the syntax here, since the cost of skipping trackBy is easy to underestimate until you watch a list actually re-render from scratch on every keystroke.

*ngSwitch is a trio, not a single directive

<div [ngSwitch]="status">
  <loading-spinner *ngSwitchCase="'loading'"></loading-spinner>
  <error-banner *ngSwitchCase="'error'"></error-banner>
  <content *ngSwitchDefault></content>
</div>

This is easy to misread as one directive, because only [ngSwitch] looks like a normal property binding. In reality, three separate directives are cooperating: [ngSwitch] sits on a container and holds the value being matched; *ngSwitchCase sits on each candidate element and independently decides whether its own value matches; *ngSwitchDefault renders only when none of the cases matched. There’s no block grouping them the way @switch/@case groups its cases visually — each *ngSwitchCase is its own structural directive, quietly watching the same [ngSwitch] value from wherever it happens to sit in the DOM.

Why you still need this

These three directives aren’t an active recommendation for new code — the previous lesson’s blocks are the better default going forward. But reading an existing Angular codebase fluently means recognizing this syntax instantly, not treating it as a translation exercise every time you open an older file. This lesson’s video walks through *ngIf and *ngFor directly; treat the microsyntax breakdown above as the reference for the parts it doesn’t dwell on.

Check yourself

  1. 01What does Angular actually do with `<li *ngFor=\"let item of items\">{{ item }}</li>` before it ever renders anything?

    It desugars the asterisk into an `<ng-template>` wrapping the `<li>`, with the directive applied to that template instead of the element directly — functionally the same `<ng-template>` + conditional-or-repeated-rendering mechanism covered earlier in this roadmap, just spelled with the asterisk shorthand instead of written out by hand.

  2. 02Why can `*ngSwitchCase` appear on multiple sibling elements without an `@case` block wrapping each one?

    Because `*ngSwitchCase` is itself a structural directive, each instance independently decides whether to render its own element based on whether its value matches the enclosing `[ngSwitch]`'s value — there's no block syntax grouping them, just several directives all watching the same switch value.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark