我正在尝试使用AngularJS ng repeat显示JSON数据,但记录正在重复。 注意:第一次加载页面时效果很好。当我尝试重新加载页面时,项目会重复。
这是我的JSON数据:
[{
"EmployeeName": "Jishnu",
"CategoryId": 0,
"Points": 76,
"DateAdded": "/Date(-62135596800000)/",
"DateModified": "/Date(-62135596800000)/",
"Id": 0
}, {
"EmployeeName": "Sini",
"CategoryId": 0,
"Points": 56,
"DateAdded": "/Date(-62135596800000)/",
"DateModified": "/Date(-62135596800000)/",
"Id": 0
}, {
"EmployeeName": "Abhilash",
"CategoryId": 0,
"Points": 12,
"DateAdded": "/Date(-62135596800000)/",
"DateModified": "/Date(-62135596800000)/",
"Id": 0
}, {
"EmployeeName": "Abhilash",
"CategoryId": 0,
"Points": 10,
"DateAdded": "/Date(-62135596800000)/",
"DateModified": "/Date(-62135596800000)/",
"Id": 0
}, {
"EmployeeName": "abhila",
"CategoryId": 0,
"Points": 6,
"DateAdded": "/Date(-62135596800000)/",
"DateModified": "/Date(-62135596800000)/",
"Id": 0
}, {
"EmployeeName": "Ajay ",
"CategoryId": 0,
"Points": 5,
"DateAdded": "/Date(-62135596800000)/",
"DateModified": "/Date(-62135596800000)/",
"Id": 0
}, {
"EmployeeName": "Abel",
"CategoryId": 0,
"Points": 4,
"DateAdded": "/Date(-62135596800000)/",
"DateModified": "/Date(-62135596800000)/",
"Id": 0
}, {
"EmployeeName": "ABDUL",
"CategoryId": 0,
"Points": 3,
"DateAdded": "/Date(-62135596800000)/",
"DateModified": "/Date(-62135596800000)/",
"Id": 0
}, {
"EmployeeName": "Administrator",
"CategoryId": 0,
"Points": 2,
"DateAdded": "/Date(-62135596800000)/",
"DateModified": "/Date(-62135596800000)/",
"Id": 0
}, {
"EmployeeName": "ABI",
"CategoryId": 0,
"Points": 1,
"DateAdded": "/Date(-62135596800000)/",
"DateModified": "/Date(-62135596800000)/",
"Id": 0
}]
这是AngularJS代码:
angular.module('myApp', [])
.controller('ReportCtrl', ['$scope', '$http', '$window', function($scope, $http, $window) {
$scope.employeePoints = [];
$scope.searchText = [];
// $http starts here
$http({
// set the parameter for request
method: 'POST',
url: 'Report/GetEmployeePoints',
headers: {
'Content-Type': 'application/json; charset=utf-8'
},
data: {}
})
.then(function(resp) {
// Success callback -- starts here
$scope.employeePoints = resp.data.response;
},
function(error) {
// Error callback
});
}]);
以下是HTML代码:
<tr ng-repeat="point in employeePoints">
<td>
<span class="name">{{point.EmployeeName}}</span>
</td>
<td class="hidden-phone">
{{point.Points}}
</td>
</tr>
以下是来自Web服务的响应的控制台日志数据:
[{"EmployeeName":"Jishnu","CategoryId":0,"Points":76,"DateAdded":"/Date(-62135596800000)/","DateModified":"/Date(-62135596800000)/","Id":280},{"EmployeeName":"Sini","CategoryId":0,"Points":56,"DateAdded":"/Date(-62135596800000)/","DateModified":"/Date(-62135596800000)/","Id":78},{"EmployeeName":"Abhilash","CategoryId":0,"Points":12,"DateAdded":"/Date(-62135596800000)/","DateModified":"/Date(-62135596800000)/","Id":100},{"EmployeeName":"Abhilash","CategoryId":0,"Points":10,"DateAdded":"/Date(-62135596800000)/","DateModified":"/Date(-62135596800000)/","Id":72},{"EmployeeName":"abhila","CategoryId":0,"Points":6,"DateAdded":"/Date(-62135596800000)/","DateModified":"/Date(-62135596800000)/","Id":323},{"EmployeeName":"Ajay ","CategoryId":0,"Points":5,"DateAdded":"/Date(-62135596800000)/","DateModified":"/Date(-62135596800000)/","Id":97},{"EmployeeName":"Abel","CategoryId":0,"Points":4,"DateAdded":"/Date(-62135596800000)/","DateModified":"/Date(-62135596800000)/","Id":297},{"EmployeeName":"ABDUL","CategoryId":0,"Points":3,"DateAdded":"/Date(-62135596800000)/","DateModified":"/Date(-62135596800000)/","Id":14},{"EmployeeName":"Administrator","CategoryId":0,"Points":2,"DateAdded":"/Date(-62135596800000)/","DateModified":"/Date(-62135596800000)/","Id":1},{"EmployeeName":"ABI","CategoryId":0,"Points":1,"DateAdded":"/Date(-62135596800000)/","DateModified":"/Date(-62135596800000)/","Id":178}]
答案 0 :(得分:0)
不确定你的情况是什么,但是下面的代码可以帮你解决问题
如果您能够将index
即id
重构为json对象,则可以使用track by
语法。
<tr ng-repeat="point in employeePoints track by point.id">
<td>
<span class="name">{{point.EmployeeName}}</span>
</td>
<td class="hidden-phone">
{{point.Points}}
</td>
</tr>
可以做到
ng-repeat="point in employeePoints track by $index"
最好使用您的实际数据ids
,如果您无法获取$index
,则可以使用angularjs ids
。