When not to use ‘track by $index’ in an AngularJS ng-repeat?

It has a disadvantage,

'track by' expression tracks by index in the array. It means that as long as the index stays the same, angularjs thinks it’s the same object.

So if you replace any object in the array, angularjs think it didn’t change because the index in the array is still the same. Because of that the change detection wont trigger when you expect it would.

Take a look at this example

Try to change the name, nothing happens.
Remove track by index, it works.
add track by item.name, it still works.

Leave a Comment