Question presented to candidate:
"Angular templates now have @if, @for and @switch blocks. Explain what they are, how they differ from the older *ngIf / *ngFor / ngSwitch structural directives, and why the team added them."
What a strong answer should cover:
- Control flow blocks are template syntax understood by the compiler, not directives: nothing to import, nothing instantiated at runtime.
- Structural directives (
*ngIf,*ngFor) are classes that receive anng-templateand create or destroy embedded views throughViewContainerRef. @ifsupports@else if/@elseinline andasaliasing;@forrequirestrackand has@emptyplus$index,$first,$last,$even,$odd,$count;@switchuses@case/@defaultwith strict equality.- Better type narrowing, fewer wrapper
ng-template/ng-containerelements, a faster list diffing algorithm. - Status: introduced in v17, stable in v18;
NgIf,NgForOfandNgSwitchare deprecated since v20 but still work in v22. - Structural directives remain the mechanism for custom structural behaviour; only the three built-ins were replaced.
Clarifying questions expected:
- "Are we talking about a new codebase, or migrating templates that already use
*ngIf?" - "Do you want the template syntax, or what the compiler generates for each?"
Code / implementation expected: Yes — a template using all three blocks, ideally with @empty and a track expression.