gpt4 book ai didi

javascript - "Duplicates in a repeater are not allowed"on ng-repeat

转载 作者:行者123 更新时间:2023-12-03 11:46:07 28 4
gpt4 key购买 nike

我从服务请求中返回了以下 json 数据:

{
"entries": [{
"id": 2081,
"name": "BM",
"niceName": "bodmas"
}]
}, {
"id": 8029,
"name": "Mas",
"niceName": "Masm"
}]
}],
"count": 2
}

我正在尝试在 html 中使用以下代码来遍历这些数据:
<option ng-repeat="entry in entries" value="{{entry.name}}">{{entry.name}}</option>

运行代码时出现以下错误:
Error: [ngRepeat:dupes] Duplicates in a repeater are not allowed. Use 'track by' expression to specify unique keys. Repeater: entry in entries, Duplicate key: string:c

以下是我的 Controller 的代码:
myApp.controller("MyController", ['$scope', '$http', '$log', function($scope, $http, $log){
...

$http.get('https://myServiceURL').success(function(data){
$scope.entries = data;
});
}]);

有人可以帮我理解为什么会出现这个错误吗?

最佳答案

添加 track by $index对您的 ng 重复,而不是:

<option ng-repeat="entry in entries" value="{{entry.name}}">{{entry.name}}</option>

尝试:
<option ng-repeat="entry in entries track by $index" value="{{entry.name}}">{{entry.name}}</option>

有更多关于这方面的信息
the documentation for this error message :

Occurs if there are duplicate keys in an ngRepeat expression. Duplicate keys are banned because AngularJS uses keys to associate DOM nodes with items.

By default, collections are keyed by reference which is desirable for most common models but can be problematic for primitive types that are interned (share references).

关于javascript - "Duplicates in a repeater are not allowed"on ng-repeat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24977103/

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