当使用$ 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保存在collectiondata item上。

  1. not track by

    在这种情况下,只要collection is updated集合, ngRepeat就会为collection is updated data item创建new uidngRepeat看到new uidsre-renders所有元素。

  2. track by,($index)

    在这种情况下, ngRepeat使用index as uid集合中数据项的index as uidngRepeat没有看到任何new uids ,因此no re-rendering元素(添加或删除,如果有的话,但no rendering of others元素)。 (由于watch保留在数据上,因此会updated但不会渲染)