【发布时间】:2017-02-11 11:05:47
【问题描述】:
我已尝试了有关此问题的所有答案,但找不到消除此错误的方法。我很确定这个过滤器函数是导致上述错误的原因。
.filter('collect_ingredients', function() {
return function(input) {
if (!angular.isArray(input)) {
return input;
}
var result = {};
angular.forEach(input, function(value){
if (!result[value.code]) {
result[value.code] = {
statement: value.statement,
code: value.code,
ingredients: [value.ingredient]
};
} else {
result[value.code].ingredients.push(value.ingredient);
}
});
return result;
};
})
我尝试将逻辑放入控制器函数中。但它也会产生同样的错误。
ionic.bundle.js:21157 Error: [$rootScope:infdig] 10 $digest() iterations reached. Aborting!
Watchers fired in the last 5 iterations: [[{"msg":"fn:
这里是负责调用上述过滤器的模板代码。
<div class="row" ng-repeat="hStatement in foo.bar|collect_ingredients">
更新
我尝试在控制器中创建一个函数,该函数接受 foo.bar 作为输入并返回更改后的列表。
<div class="row" ng-repeat="hStatement in getIngredientsFromFilter(foo.bar)">
controller.js:
$scope.getIngredientsFromFilter = function(input_lst) {
return $filter("collect_ingredients")(input_lst);
}
filter.js:
.filter('collect_ingredients', function() {
return function(input) {
if (!angular.isArray(input)) {
return input;
}
var result_lst = [];
var result_dict = {};
angular.forEach(input, function(value){
var dict = {};
if (!result_dict[value.code]) {
dict = {
statement: value.statement,
code: value.code,
ingredients: [value.ingredient]
};
result_dict[value.code] = dict;
result_lst.push(dict);
} else {
result_dict[value.code].ingredients.push(value.ingredient);
result_lst.push(result_dict[value.code]);
}
});
return result_lst;
};
})
但这也会产生同样的错误..
【问题讨论】:
-
你怎么称呼过滤器?
-
@sheilak 添加...