gpt4 book ai didi

angularjs - Angular ng 重复错误 "Duplicates in a repeater are not allowed."

转载 作者:行者123 更新时间:2023-12-02 21:38:48 24 4
gpt4 key购买 nike

我正在定义一个自定义过滤器,如下所示:

<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 指令中存在重复项。这意味着如果您尝试执行以下操作,您将收到错误。

// 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

关于angularjs - Angular ng 重复错误 "Duplicates in a repeater are not allowed.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16296670/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com