I've got the following json data returned from a service request:
{
"entries": [{
"id": 2081,
"name": "BM",
"niceName": "bodmas"
}]
}, {
"id": 8029,
"name": "Mas",
"niceName": "Masm"
}]
}],
"count": 2
}
And I am trying the following code in html to loop through this data:
<option ng-repeat="entry in entries" value="{{entry.name}}">{{entry.name}}</option>
I get the following error when I run the code:
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
Following is the code for my controller:
myApp.controller("MyController", ['$scope', '$http', '$log', function($scope, $http, $log){
...
$http.get('https://myServiceURL').success(function(data){
$scope.entries = data;
});
}]);
Could somebody help me understand why am I getting that error?
duplicates in In ng-repeat is not allowed . Example
Your JSON is invalid and should be :
Also, make sure you are accessing the document at the right level, use :
Then, it should work. See this JSBin.
It looks like you have a problem with the structure of the data in your scope. Your example JSON shows an object with an
entries
property and acount
property. You then put that whole object in your scope asentries
. This means you'd need to access the entries asentries.entries
, with the count inentries.count
. Perhaps this controller is closer to what you wanted:// To allow this Web Service to be called from script, using ASP.NET AJAX,uncomment the following line.
[System.Web.Script.Services.ScriptService] ==> Uncomment this line If you use .NET Service
Add
track by $index
to your ng repeat so instead of:Try:
There's further information about this in the documentation for this error message:
If I may add an additional reason as to why this can occur...
If you are doing this with a JS object [] or {}
and you are passing it in to a directive like this
Inside the directive you must turn myObject back into an object by doing this
Then the HTML and ng-repeat will work
ngRepeat does not know how to repeat over a single string.
Here is what the object would look like before $scope.$eval
and after $scope.$eval()
The error kind of makes a reference that it cannot repeat of a string. Here is the error that I got.