Why is track important in @for (or trackBy in *ngFor)?
Model answer
When a list changes, Angular needs to know which items are the same so it can reuse their DOM elements instead of destroying and recreating all of them. The track expression gives each item a stable identity, usually its id: @for (user of users; track user.id).
Without a good track value, a refreshed list can lose focus, scroll position and input state, and can be slow. Never track by index for lists that can be reordered or filtered, unless the items truly have no id.
Learn this topic
Angular Tutorial: Beginner to Advanced .
Sign in to add your own answer.
More questions
- What are the main ways to improve Angular app performance?
- Why is calling a method in a template expression a performance problem?
- What is NgOptimizedImage and what does it give you?
- What is server-side rendering (SSR), and why would you use it?
- What is hydration in Angular?
- What is the difference between SSR, SSG (prerendering) and CSR, and how do you choose?