Question presented to candidate:
"In Angular's @for block, track is mandatory, unlike the old trackBy. Why did the team make it required, what should you track by, and what does the @empty block give you?"
What a strong answer should cover:
trackgives each item an identity, so Angular can match old rows to new rows and move or keep their DOM instead of destroying and recreating it.- Without a good key, a refetch that returns new objects recreates every row, losing focus, input text, scroll position, animations and child component state.
- Good keys: a stable unique id (
track item.id).track $indexis only right for static lists;track item(identity) fails when objects are recreated. - Dev-mode diagnostics: NG0955 (duplicate keys) and NG0956 (the whole collection was recreated).
- Making
trackmandatory fixes the most common performance mistake of*ngFor, wheretrackBywas optional and usually forgotten. @emptyrenders when the collection is empty,nullorundefined, so an empty state needs no separate@ifand cannot drift out of sync with the loop.
Clarifying questions expected:
- "Does the data come from an API that returns fresh objects on every call?"
- "Can the list be reordered, filtered or edited in place?"
Code / implementation expected: Yes β a list showing the difference between a real key and $index, with an @empty state.