Components and templates · core
Built-in control flow, track and pipes
The built-in @if, @for and @switch blocks replaced *ngIf and *ngFor in new code. @for needs track, and a pure pipe is the right place for template formatting.
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceThe notes
What you must know
@if,@else,@forand@switchare template blocks, not directives. New code uses them instead of*ngIfand*ngFor(facts.md).@forrequires atrackexpression. It tells Angular how to recognise an item between renders.track orderuses object identity. If every refresh returns new objects, every row is destroyed and created again: lost focus, lost input state, slow work. Dev mode warns with NG0956.- Fix:
track order.id, a stable unique key. Duplicate keys give wrong results. - A method call in a template runs on every check. A pure pipe runs again only when its input changes by reference.
- A pipe is a class with
@Pipe({ name })andtransform. It is standalone and goes into the componentimports. *ngIfand*ngForstill work in old code. Say which is old.
The red pen
Common traps: mistake → fix
Mistake:
track $indexis always safeFix: it is fine for a list that never reorders, but it reuses the wrong DOM when items move or are inserted.
Mistake: A pure pipe runs on every check
Fix: it runs only when its input reference changes.
Mistake:
@foris the same as*ngForwith new syntaxFix:
trackis required and the block is faster and built in.
Easy to hard
Questions interviewers ask, easy to hard
- How do you show a list in a modern Angular template?
- Why does
@forneedtrack? - A list loses input focus every time it refreshes. Why?
- When would you write a pipe instead of calling a method in the template?
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceYour first interview is free
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.