1.

What is the importance of track by in the ng-repeat directive?

Answer»

ng-repeat DIRECTIVE helps to keep track of all DOM elements dynamically to minimize DOM creation and rendering. It is achieved by storing the instances of the object whenever a new element gets added to the LIST or collection. AngularJS just renders the newly added element instead of re-rendering the overall collection. This helps in rendering the elements faster.

In case the ng-repeat is operated on a collection that has the objects of unique identifier, the tracking of new elements should be done based on that ID instead of new instance insertion. This can be done by making use of the track by provided by the ng-repeat. For example, in the below piece of code:

<DIV ng-repeat="obj in objectList track by obj.id"> <!--Some code --></div>

the tracking of new elements is done using the object’s id.



Discussion

No Comment Found