Angular JSON - 使用'track by'表达式

Ana*_*ngh 0 javascript php mysql json angularjs

我在angular + Php + MySql中制作一个CRUD,我遇到了一个问题.我在数据库中有一些数据,并希望在列表页面上显示它.

为了实现这一点,我在Angular中进行了ajax调用,并将响应放在Scope Variable中.如果我打印响应它显示我低于响应但不能用HTML显示它.我遇到了一个问题,即: -

错误:[ngRepeat:dupes]不允许在转发器中重复.使用'track by'表达式指定唯一键.中继器:机组人员,重复键:字符串:",重复值:"\""

以下是Angular和HTML代码: -

.controller('wrapperController', ['$scope','$rootScope','$http', function(scope,rootScope,http){

    http.post("server/viewRecords.php")
    .success(function(response){
        console.log(response);
        scope.crew = response;

    });
}])

<tr ng-repeat="person in crew">
            <td>{{person.name}}</td>
            <td>{{person.desc}}</td>
            <td> <a href="#/edit/{{$index}}">Edit</a> </td>
            <td> <a href="#/delete/{{$index}}" data-ng-click="deleteRecord($index)">Delete</a> </td>
        </tr>
Run Code Online (Sandbox Code Playgroud)

请提出为什么我收到此错误?

谢谢!!

lva*_*yut 5

AngularJS不允许在ng-repeat指令中重复.因此,您可以添加track by $index到ng-repeat中,如下所示:

<tr ng-repeat="person in crew track by $index">
            <td>{{person.name}}</td>
            <td>{{person.desc}}</td>
            <td> <a href="#/edit/{{$index}}">Edit</a> </td>
            <td> <a href="#/delete/{{$index}}" data-ng-click="deleteRecord($index)">Delete</a> </td>
</tr>
Run Code Online (Sandbox Code Playgroud)

Track by $index告诉角度使用$index,即自动增量,作为键而不是数组中的每个单独元素.所以,它不会再重复了.