Question presented to candidate:
"We have a few hundred templates full of *ngIf, *ngFor and ngSwitch. Walk me through how each one maps to the new @if, @for and @switch blocks, and how you would migrate the codebase safely."
What a strong answer should cover:
- The mapping:
*ngIf="x as y; else tpl"becomes@if (x; as y) { } @else { };*ngFor="let t of list; trackBy: fn; let i = index"becomes@for (t of list; track t.id; let i = $index) { };[ngSwitch]/*ngSwitchCase/*ngSwitchDefaultbecome@switch/@case/@default. - Loop context variables gain a
$prefix ($index,$first,$last,$even,$odd,$count). trackis mandatory, and an existingtrackByfunction can be called from it.- The official schematic:
ng generate @angular/core:control-flow, what it rewrites and what it leaves (it also removes the now-unused directive imports). - The old directives are deprecated since v20 but still work, so the migration can be gradual.
- Verify after migrating: compile with strict templates and test lists, because
@foruses a different diffing algorithm.
Clarifying questions expected:
- "Is there a test suite I can rely on to catch behaviour changes after the schematic runs?"
- "Do any templates use custom structural directives? Those are not touched by the migration."
Code / implementation expected: Yes β a before and after of the same component.