Angular ng-repeat 错误“不允许在中继器中重复." [英] Angular ng-repeat Error "Duplicates in a repeater are not allowed."
问题描述
我正在定义一个自定义过滤器,如下所示:
<div class="section comment clearfix" ng-repeat="comment in item.comments | range:1:2">....
如您所见,使用过滤器的 ng-repeat 嵌套在另一个 ng-repeat 中
过滤器定义如下:
myapp.filter('range', function() {返回函数(输入,最小值,最大值){min = parseInt(min);//使字符串输入为int最大 = parseInt(max);for (var i=min; i
我得到:
<块引用>错误:不允许在转发器中重复.转发器:在 item.comments 中评论 |范围:1:2 ngRepeatAction@https://ajax.googleapis.com/ajax/libs/angularjs/1.1.4/an
解决方案其实描述在这里:http://www.anujgakhar.com/2013/06/15/duplicates-in-a-repeater-are-not-allowed-in-angularjs/
AngularJS 不允许在 ng-repeat 指令中重复.这意味着,如果您尝试执行以下操作,则会出现错误.
//此代码抛出错误不允许在转发器中重复.//中继器:[1,1,1] 中的行 key: number:1"<div ng-repeat="row in [1,1,1]">
但是,稍微更改上面的代码以定义一个索引来确定唯一性,如下所示将使其再次工作.
//这会起作用<div ng-repeat="row in [1,1,1] track by $index">
官方文档在这里:https://docs.angularjs.org/error/ngRepeat/dupes
I am defining a custom filter like so:
<div class="idea item" ng-repeat="item in items" isoatom>
<div class="section comment clearfix" ng-repeat="comment in item.comments | range:1:2">
....
</div>
</div>
As you can see the ng-repeat where the filter is being used is nested within another ng-repeat
The filter is defined like this:
myapp.filter('range', function() {
return function(input, min, max) {
min = parseInt(min); //Make string input int
max = parseInt(max);
for (var i=min; i<max; i++)
input.push(i);
return input;
};
});
I'm getting:
Error: Duplicates in a repeater are not allowed. Repeater: comment in item.comments | range:1:2 ngRepeatAction@https://ajax.googleapis.com/ajax/libs/angularjs/1.1.4/an
The solution is actually described here: http://www.anujgakhar.com/2013/06/15/duplicates-in-a-repeater-are-not-allowed-in-angularjs/
AngularJS does not allow duplicates in a ng-repeat directive. This means if you are trying to do the following, you will get an error.
// This code throws the error "Duplicates in a repeater are not allowed.
// Repeater: row in [1,1,1] key: number:1"
<div ng-repeat="row in [1,1,1]">
However, changing the above code slightly to define an index to determine uniqueness as below will get it working again.
// This will work
<div ng-repeat="row in [1,1,1] track by $index">
Official docs are here: https://docs.angularjs.org/error/ngRepeat/dupes
这篇关于Angular ng-repeat 错误“不允许在中继器中重复."的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!