Understanding the ngRepeat 'track by' expression

后端 未结 3 1532
醉话见心
醉话见心 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条回答
  •  南方客
    南方客 (楼主)
    2020-11-29 18:54

    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).

    WON'T WORK:

    
      // something with item ...
    
    

    but you can, if using track by $index:

    
      // something with item ...
    
    

提交回复
热议问题