以下是代码:plnkr.co
HTML:
<!DOCTYPE html>
<html ng-app="example">
<head>
<script data-require="jquery@1.11.3" data-semver="1.11.3" src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
<script data-require="angular.js@1.2.28" data-semver="1.2.28" src="https://code.angularjs.org/1.2.28/angular.js"></script>
</head>
<body ng-controller="mainCtrl">
<div ng-repeat="(key, val) in obj | groupPlayers">
{{key}} = {{ val }}
</div>
<script type="text/javascript" src="script.js"></script>
</body>
</html>
的script.js
(function() {
angular
.module('example', [])
.controller('mainCtrl', function($scope) {
$scope.obj = [
{ lastname: 'Jackson', firstname: 'George', score: 6},
{ lastname: 'Jackson', firstname: 'George', score: 10},
{ lastname: 'Smith', firstname: 'Michael', score: 8},
{ lastname: 'Smith', firstname: 'Amanda', score: 2},
{ lastname: 'Smith', firstname: 'Michael', score: 7},
{ lastname: 'Doe', firstname: 'John', score: 4},
];
})
.filter('groupPlayers', function() {
return function(list, search) {
var group = {};
var key;
angular.forEach(list, function(item) {
key = item.lastname + ' ' + item.firstname;
if(!group[key])
group[key] = [];
group[key].push(item.score);
});
return group;
}
})
;
})();
我想将对象的项目分组以按玩家显示得分玩家。我创建了一个角度滤镜来完成这项工作。显示很好,但我在控制台中出错,经过一小时的分析后我没有发现问题!
错误:达到10 $ digest()次迭代。中止!
由于
答案 0 :(得分:1)
使用<div ng-repeat="(key, val) in objFiltered">
和$scope.objFiltered = $filter('groupPlayers')($scope.obj);
时,错误不再发生。