ng-使用重复条目重复重复数据

ng -repeat repeating data with duplicate entries

本文关键字:数据 ng-      更新时间:2023-09-26

我试图使用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}]

不确定您的情况是什么,但下面的代码将为您完成

如果能够将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,如果无法获取ids,也可以使用angularjs $index