我定义了一个自定义过滤器,如下所示:
<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>
正如您所看到的,使用过滤器的ng-repeat嵌套在另一个ng-repeat中
过滤器的定义如下:
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;
};
});
我得到:
错误:在中继器中不允许重复。Repeater: comment in item.comments | range: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指令重复。这意味着如果您尝试执行以下操作,将会得到一个错误。
// 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]">
但是,稍微改变一下上面的代码,定义一个索引来确定唯一性,如下所示,它将重新工作。
// This will work
<div ng-repeat="row in [1,1,1] track by $index">
官方文档在这里:https://docs.angularjs.org/error/ngRepeat/dupes
你想让你的“range”过滤器做什么?
以下是我认为您正在尝试做的工作示例:http://jsfiddle.net/evictor/hz4Ep/
HTML:
<div ng-app="manyminds" ng-controller="MainCtrl">
<div class="idea item" ng-repeat="item in items" isoatom>
Item {{$index}}
<div class="section comment clearfix" ng-repeat="comment in item.comments | range:1:2">
Comment {{$index}}
{{comment}}
</div>
</div>
</div>
JS:
angular.module('manyminds', [], function() {}).filter('range', function() {
return function(input, min, max) {
var range = [];
min = parseInt(min); //Make string input int
max = parseInt(max);
for (var i=min; i<=max; i++)
input[i] && range.push(input[i]);
return range;
};
});
function MainCtrl($scope)
{
$scope.items = [
{
comments: [
'comment 0 in item 0',
'comment 1 in item 0'
]
},
{
comments: [
'comment 0 in item 1',
'comment 1 in item 1',
'comment 2 in item 1',
'comment 3 in item 1'
]
}
];
}
解决方案实际上描述在这里:http://www.anujgakhar.com/2013/06/15/duplicates-in-a-repeater-are-not-allowed-in-angularjs/
AngularJS不允许ng-repeat指令重复。这意味着如果您尝试执行以下操作,将会得到一个错误。
// 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]">
但是,稍微改变一下上面的代码,定义一个索引来确定唯一性,如下所示,它将重新工作。
// This will work
<div ng-repeat="row in [1,1,1] track by $index">
官方文档在这里:https://docs.angularjs.org/error/ngRepeat/dupes