我有以下代码重复并显示用户的姓名和他的分数:
<div ng-controller="AngularCtrl" ng-app>
<div ng-repeat="user in users | orderBy:predicate:reverse | limitTo:10">
<div ng-init="user.score=user.id+1">
{{user.name}} and {{user.score}}
</div>
</div>
</div>
以及相应的角度控制器。
function AngularCtrl($scope) {
$scope.predicate = 'score';
$scope.reverse = true;
$scope.users = [{id: 1, name: 'John'}, {id: 2, name: 'Ken'}, {id: 3, name: 'smith'}, {id: 4, name: 'kevin'}, {id: 5, name: 'bob'}, {id: 6, name: 'Dev'}, {id: 7, name: 'Joe'}, {id: 8, name: 'kevin'}, {id: 9, name: 'John'}, {id: 10, name: 'Ken'}, {id: 11, name: 'John'}, {id: 1, name: 'John'}, {id: 2, name: 'Ken'}, {id: 3, name: 'smith'}, {id: 4, name: 'kevin'}, {id: 5, name: 'bob'}, {id: 6, name: 'Dev'}, {id: 7, name: 'Joe'}, {id: 8, name: 'kevin'}, {id: 9, name: 'John'}, {id: 10, name: 'Ken'}]
}
当我运行上面的代码时,我得到了错误:10 $digest() 达到了迭代。中止!我的控制台中的错误。
我已经为此创建了jsfiddle。
排序谓词仅在 ng-repeat 内部初始化,并且限制应用于对象的数量。所以我觉得同时拥有 sortby 和 limitTo 观察者是错误的原因。
如果 $scope.reverse 为假(分数升序),则不会出错。
谁能帮我理解这里有什么问题?非常感谢您的帮助。