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
的跟踪时,它只被调用一次。为什么会这样?对此有任何想法。
答案 0 :(得分:1)
使用track by $index
时。观看位于data item
collection
index
,uid
而不是not track by
。
ngRepeat
,
在这种情况下,只要new uid
data item
collection is updated
为集合中的ngRepeat
创建new uids
。 re-renders
track by,($index)
和ngRepeat
所有元素。
index as uid
在这种情况下,ngRepeat
将new 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;
}
}
}
但不会呈现