问题

我要定义一个自定义过滤器:

<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;
    };
});

我得到:

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



解决方法

解决方案实际上描述如下: http://www.anujgakhar.com/2013/06/15/duplicates-in-a-repeater-are-not-allowed-in-angularjs/

AngularJS不允许在ng-repeat指令中重复.这意味着如果您尝试执行以下操作,您将收到错误.

// the below will throw 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 < / p>




相关问题推荐