Understanding the ngRepeat 'track by' expression

后端 未结 3 1531
醉话见心
醉话见心 2020-11-29 18:25

I\'m having difficulties understanding how the track by expression of ng-repeat in angularjs works. The documentation is very scarce: http://docs.angularjs.

3条回答
  •  -上瘾入骨i
    2020-11-29 18:49

    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:

    http://www.bennadel.com/blog/2556-using-track-by-with-ngrepeat-in-angularjs-1-2.htm

    a more practical guide:

    http://www.codelord.net/2014/04/15/improving-ng-repeat-performance-with-track-by/

    (track by is available in angular > 1.2 )

提交回复
热议问题