Complete the code to add a trackBy function to the ngFor directive.
<div *ngFor="let item of items; trackBy: [1]">{{item.name}}</div>
The trackBy function helps Angular track items by a unique identifier, improving performance. Here, trackById is the function name used.
Complete the trackBy function to return the unique id of the item.
trackById(index: number, item: any): number {
return [1];
}The trackBy function should return a unique identifier for each item. Returning item.id ensures Angular can track items efficiently.
Fix the error in the trackBy function to correctly return the unique id.
trackById(index: number, item: any): number {
return [1];
}item.id() as if it were a function.The correct way to access the id property is item.id. Using item.id() calls a function which does not exist and causes an error.
Fill both blanks to complete the ngFor with trackBy and the trackBy function definition.
<div *ngFor="let user of users; trackBy: [1]">{{user.name}}</div> [2] trackById(index: number, user: any): number { return user.id; }
const or let incorrectly for function declaration.The trackBy directive uses the trackById function. The function is declared with the function keyword in the component.
Fill all three blanks to create a trackBy function that tracks by user id and use it in ngFor.
<ul> <li *ngFor="let user of users; trackBy: [1]">{{user.name}}</li> </ul> [2] trackById([3]: number, user: any): number { return user.id; }
index.The trackBy directive uses the trackById function. The function is declared with the function keyword and takes index as the first parameter.