J'ai une ng-repeat placée dans une liste à afficher dans le tableau.ng-repeat fonctionne parfaitement avec json statique Mais fire exception avec des données dynamiques json
le code html script est comme ceci:
`<tr ng-repeat="user in users track by $index" ng-class="{'selected':$index == selectedRow}">
<td>
{{user.FirstName}}
</td>
<td>
{{user.LastName}}
</td>
<td>
{{user.EmailId}}
</td>
<td>
{{user.ContactNumber}}
</td>
<td>
<button type="button" class="btn btn-warning">
<span class="glyphicon glyphicon-pencil"></span>
</button>
<button type="button" class="btn btn-danger">
<span class="glyphicon glyphicon-remove"></span>
</button>
</td>
</tr>`
et dans le script angulaire du routage est défini comme ci-dessous:
var routingApp = angular.module('App', []);
routingApp.controller("MovieController", ['$scope', '$http', function ($scope, $http) {
$scope.edit = false;
$scope.error = false;
$scope.success = false;
//TO DO : Below portion to be replaced with ajax call to get list of movies from DB
var users = [
{ "UserId": 1, "FirstName": "user", "LastName": "1", "EmailId": "[email protected]", "ContactNumber": 1234567890, "UserName": "User1", "Password": "[email protected]" },
{ "UserId": 2, "FirstName": "user", "LastName": "2", "EmailId": "[email protected]", "ContactNumber": 7894612305, "UserName": "User2", "Password": "[email protected]" },
{ "UserId": 3, "FirstName": "user", "LastName": "3", "EmailId": "[email protected]", "ContactNumber": 1472583690, "UserName": "User3", "Password": "[email protected]" },
{ "UserId": 4, "FirstName": "user", "LastName": "4", "EmailId": "[email protected]", "ContactNumber": 7531598520, "UserName": "User4", "Password": "[email protected]" },
{ "UserId": 5, "FirstName": "User", "LastName": "5", "EmailId": "[email protected]", "ContactNumber": 4785961235, "UserName": "user5", "Password": "[email protected]" }
];
console.log(users); //debugger;
$scope.users = users;
}]);
Cette partie fonctionne tout à fait bien.
mais quand je remplace les données json statiques c.-à-d.
var users = [
{ "UserId": 1, "FirstName": "user", "LastName": "1", "EmailId": "[email protected]", "ContactNumber": 1234567890, "UserName": "User1", "Password": "[email protected]" },
{ "UserId": 2, "FirstName": "user", "LastName": "2", "EmailId": "[email protected]", "ContactNumber": 7894612305, "UserName": "User2", "Password": "[email protected]" },
{ "UserId": 3, "FirstName": "user", "LastName": "3", "EmailId": "[email protected]", "ContactNumber": 1472583690, "UserName": "User3", "Password": "[email protected]" },
{ "UserId": 4, "FirstName": "user", "LastName": "4", "EmailId": "[email protected]", "ContactNumber": 7531598520, "UserName": "User4", "Password": "[email protected]" },
{ "UserId": 5, "FirstName": "User", "LastName": "5", "EmailId": "[email protected]", "ContactNumber": 4785961235, "UserName": "user5", "Password": "[email protected]" }
];
avec
$http.get("http://localhost:82/api/User/GetAllUsers")
.then(function (response) {
dt = JSON.stringify(response.data);
$scope.users = dt;
//console.log(dt);
//console.log(JSON.parse(dt));
})
ce sera par cette exception:
`[ngRepeat:dupes] Duplicates in a repeater are not allowed. Use 'track by' expression to specify unique keys. Repeater: user in users, Duplicate key: string:", Duplicate value`
Mais la sortie du api est exactement le même que JSON statique, en fait je l'ai copié JSON de sortie api lui-même.
Même j'ai essayé d'enlever track by $index
mais pas de chance. existe-t-il des solutions à ce problème?
Essayez cette '.alors (function (réponse) {$ scope.users = response.data; })' –