[Solved] Understanding the ngRepeat ‘track by’ expression

I’m having difficulties understanding how the track by expression of ng-repeat in angularjs works. The documentation is very scarce: http://docs.angularjs.org/api/ng/directive/ngRepeat

Can you explain what the difference between those two snippets of code is in terms of databinding and other relevant aspects?

with: track by $index

<!--names is an array-->
<div ng-repeat="(key, value) in names track by $index">
  <input ng-model="value[key]">                         

without (same output)

<!--names is an array-->
<div ng-repeat="(key, value) in names">
   <input ng-model="value[key]">                         

Solution #1:

You can track by $index if your data source has duplicate identifiers

e.g.: $scope.dataSource: [{id:1,name:'one'}, {id:1,name:'one too'}, {id:2,name:'two'}]

You can’t iterate this collection while using ‘id’ as identifier (duplicate id:1).


<element ng-repeat="item.id as item.name for item in dataSource">
  // something with item ...

but you can, if using track by $index:

<element ng-repeat="item in dataSource track by $index">
  // something with item ...
Respondent: nilsK

Solution #2:

a short summary:

track by is used in order to link your data with the DOM generation (and mainly re-generation) made by ng-repeat.

when you add track by you basically tell angular to generate a single DOM element per data object in the given collection

this could be useful when paging and filtering, or any case where objects are added or removed from ng-repeat list.

usually, without track by angular will link the DOM objects with the collection by injecting an expando property – $$hashKey – into your JavaScript objects, and will regenerate it (and re-associate a DOM object) with every change.

full explanation:


a more practical guide:


(track by is available in angular > 1.2 )

Respondent: Nuriel

Solution #3:

If you are working with objects track by the identifier(e.g. $index) instead of the whole object and you reload your data later, ngRepeat will not rebuild the DOM elements for items it has already rendered, even if the JavaScript objects in the collection have been substituted for new ones.

Respondent: ram1993

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Leave a Reply

Your email address will not be published.