当使用$ index跟踪时,ng-init没有第二次调用
HTML:
{{employeeName}}
控制器:
$scope.initUserDecision = function(row,index){ $scope.employeeName=row["name"]; } $scope.rows=[{id:1,name:'siva'},{id:2,name:'ram'}] //changing $scope.rows in button click event and used $scope.$apply as well angular.element(document).on("click", "#change", function () { $scope.rows=[{id:1,name:'ravi'},{id:2,name:'raj'}] $scope.$apply(); });
ng-init
函数在tr初始化时首次调用。 如果我点击更改按钮行集合被更改,它将不会再次调用ng-init
。 如果我通过$index
ng-init
删除跟踪调用时间。 当我通过$index
使用track时,它只被调用了一次。 为什么会这样? 对此有任何想法。
当您使用track by $index
。 Watch通过index
而不是uid
保存在collection
的data item
上。
-
not track by
,在这种情况下,只要
collection is updated
集合,ngRepeat
就会为collection is updated
data item
创建new uid
。ngRepeat
看到new uids
并re-renders
所有元素。 -
track by,($index)
在这种情况下,
ngRepeat
使用index as uid
集合中数据项的index as uid
。ngRepeat
没有看到任何new uids
,因此no re-rendering
元素(添加或删除,如果有的话,但no rendering of others
元素)。 (由于watch
保留在数据上,因此会updated
但不会渲染)