Angular FAQ: Top Questions
28. What is the purpose of trackBy in *ngFor?
Using trackBy
with *ngFor
improves performance by avoiding unnecessary DOM updates. It lets Angular identify items uniquely when re-rendering.
<li *ngFor="let user of users; trackBy: trackById">{{user.name}}</li>
trackById(index: number, user: any): number {
return user.id;
}
- Prevents full re-rendering of lists on each change.