我从服务请求中返回了以下 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).