Bird
0
0

Which of the following is the correct syntax to use trackBy in an ngFor loop?

easy📝 Syntax Q12 of 15
Angular - Performance Optimization
Which of the following is the correct syntax to use trackBy in an ngFor loop?
A<code>*ngFor="let item of items; trackByFn"</code>
B<code>*ngFor="let item of items; trackBy: trackByFn"</code>
C<code>*ngFor="let item of items; trackByFn()"</code>
D<code>*ngFor="let item of items; trackBy=trackByFn()"</code>
Step-by-Step Solution
Solution:
  1. Step 1: Recall the correct ngFor syntax with trackBy

    The correct syntax uses a semicolon and the keyword trackBy followed by the function name without parentheses.
  2. Step 2: Compare options

    *ngFor="let item of items; trackBy: trackByFn" matches the correct syntax: *ngFor="let item of items; trackBy: trackByFn". Others either miss the keyword or use parentheses incorrectly.
  3. Final Answer:

    *ngFor="let item of items; trackBy: trackByFn" -> Option B
  4. Quick Check:

    Correct syntax includes 'trackBy:' keyword [OK]
Quick Trick: Use 'trackBy:' keyword with function name, no parentheses [OK]
Common Mistakes:
  • Omitting 'trackBy:' keyword
  • Adding parentheses after function name
  • Using '=' instead of ':'

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes