Skip to content

trackBy

Without trackBy, Angular destroys and recreates all DOM nodes when the array reference changes. trackBy identifies items by a stable key.

@Component({
template: `
<li *ngFor="let user of users; trackBy: trackByUserId">
{{ user.name }}
</li>
`
})
export class UsersComponent {
trackByUserId(index: number, user: User): number {
return user.id;
}
}

Always use trackBy with *ngFor for lists with more than a few items.”