angularjs - 更新范围时,NG-Repeat 不会干净地更新

标签 angularjs angularjs-ng-repeat ng-repeat

所以我有一个工作区列表:

<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 函数。

issue

最佳答案

试试这个……

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/

相关文章:

javascript - Angular 将字符串传递给函数

javascript - 对象未更新 Angular

javascript - Angularjs删除奇偶选定行中具有不同背景颜色的表行

angularjs - 为什么我不能用 ng-style 设置元素的高度?

javascript - 如何等待函数在 Angular 2 中完成执行?

javascript - Angular View 中递增数字(级联 ng-repeat)

javascript - 无法将数据从 Controller 读取到 View 中

angularjs - 如何在指令模板 Angular JS 中使用 ng-repeat

javascript - AngularJS ng-repeat 在数组更新时不刷新

angularjs - AngularJS:2 x <tr>的ng-repeat-无法使用DIV