What do @if, @for and @switch do, and why was this syntax added?
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
- Mention track and @empty to show real use.
Learn this topic
Angular Tutorial: Beginner to Advanced .
Sign in to add your own answer.
More questions
- What are the parts of a component?
- What is data binding? Explain the four types.
- What is the difference between interpolation and property binding?
- How do you pass data from a parent component to a child component?
- How does a child component send data to its parent?
- What is the component lifecycle? Describe the main hooks.