← BYTES
AngularJS·Intermediate·Ref: 5.2.7

What is the basic purpose of 'track by' in an expression like ng-repeat="item in items track by item.id"?

Have a question about this video? Mention reference 5.2.7 when you contact us.

a.It sorts the items alphabetically by that property
b.It is required syntax with no functional effect
c.It filters out items where that property is falsy
d.It tells AngularJS how to identify whether an item is 'the same' across digests, so it can avoid unnecessarily destroying and recreating DOM elements for items that haven't actually changed identity
Without track by, ng-repeat by default tracks items by object identity, which breaks (causing full re-render) when the underlying array is replaced with a new array of structurally-identical-but-different objects on every update; track by lets you specify a stable key (like an id) so AngularJS can correctly match old and new items and reuse their DOM nodes.

Find this on YouTube & Instagram

Every BYTES video is tagged with its reference number and topic — search any of these on YouTube or Instagram to find this one:

#BYTES#BYTES527#AngularJS