在使用$ index跟踪时,ng-init没有第二次调用

时间:2016-10-11 12:07:47

标签: javascript jquery angularjs angularjs-ng-repeat angularjs-track-by

HTML:

<table>
  <tbody>
    <tr ng-repeat="row in Rows  track by $index" ng-init="initUserDecision(row,$index)" >
      <td>{{employeeName}}</td>
    </tr>
</table>
<button id="change"/>

控制器:

$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();
});
当tr初始化时,

ng-init函数首次调用。如果我点击更改按钮行集合被更改,它不会再次调用ng-init。如果我通过$index ng-init删除跟踪时间。当我使用$index的跟踪时,它只被调用一次。为什么会这样?对此有任何想法。

1 个答案:

答案 0 :(得分:1)

使用track by $index时。观看位于data item collection indexuid而不是not track by

  1. ngRepeat

    在这种情况下,只要new uid data item collection is updated为集合中的ngRepeat创建new uidsre-renders track by,($index)ngRepeat所有元素。

  2. index as uid

    在这种情况下,ngRepeatnew uids用于集合中的数据项。 no re-rendering没有看到任何no rendering of others因此watch元素(添加或删除,如果有的话,只有updated)。 (由于upstream api { server 192.168.0.15:9998; } 保留在数据上,它将获得events { worker_connections 4096; ## Default: 1024 } http { # Change this depending on environment upstream api { server 192.168.0.1:9998; } server { listen 80; server_name localhost; root /usr/share/nginx/html; index index.html index.htm; include /etc/nginx/mime.types; location / { # If you want to enable html5Mode(true) in your angularjs app for pretty URL # then all request for your angularJS app will be through index.html try_files $uri /index.html; } # /api will server your proxied API that is running on same machine different port # or another machine. So you can protect your API endpoint not get hit by public directly location /api { proxy_pass http://api; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 'upgrade'; proxy_set_header Host $host; proxy_cache_bypass $http_upgrade; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } #Static File Caching. All static files with the following extension will be cached for 1 day location ~* .(jpg|jpeg|png|gif|ico|css|js)$ { expires 1d; } } } 但不会呈现