What do @if, @for and @switch do, and why was this syntax added?

Angular components and templates · Freshers and experienced

Model answer

They are the built-in control flow in templates. @if shows a block when a condition is true (with @else), @for repeats a block for each item in a collection, and @switch chooses between cases. @for requires a track expression, such as track item.id, which helps Angular reuse DOM elements efficiently, and it has an @empty block.

They replaced *ngIf, *ngFor and *ngSwitch for new code. The new syntax is easier to read, needs no imports, and is faster because the compiler knows more about it.

Tips

Learn this topic

Angular Tutorial: Beginner to Advanced .

Sign in to add your own answer.

More questions