所以我有一个工作区列表:
<li class="workspace-object" ng-repeat="w in workspaces | filter:searchQuery" ng-click="selectWorkspace(w)">
<a href="" class="workspace-link">{{w.name | titleCase }}</a>
</li>
该列表通过某个函数在数据库中更新,然后我调用下面的 loadWorkspaces 函数,重新填充 $scope.workspaces 对象
$scope.loadWorkspaces = function() {
APIService.getWorkspaces().then(function(data){
$scope.workspaces = data;
});
}
发生这种情况时,$scope.workspaces 在注销时会立即读取正确的更新信息,但是,在更新到正确的列表之前,ng-repeat 会重复读取。我不知道为什么会这样。有任何想法吗? 在此示例中,我正在更新工作区标题,并且该更新运行 loadworkspaces 函数。
最佳答案
试试这个……
w in workspaces track by $index
When the contents of the collection change, ngRepeat makes the corresponding > changes to the DOM:
- When an item is added, a new instance of the template is added to the DOM.
- When an item is removed, its template instance is removed from the DOM.
- When items are reordered, their respective templates are reordered in the DOM.
By default, ngRepeat does not allow duplicate items in arrays. This is because when there are duplicates, it is not possible to maintain a one-to-one mapping between collection items and DOM elements.
If you do need to repeat duplicate items, you can substitute the default tracking behavior with your own using the track by expression.
For example, you may track items by the index of each item in the collection, using the special scope property $index
关于angularjs - 更新范围时,NG-Repeat 不会干净地更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29972365/