简体   繁体   English

ngRepeat:dupes-在对象数组上

[英]ngRepeat:dupes - over an object array

ng-repeat showing "Duplicate Key in Repeater" over JSON object array. ng-repeat在JSON对象数组上显示“ Repeater中的重复密钥”。 "track by $index" is also not working. “按$ index跟踪”也不起作用。

Angular code 角度代码

        result = doAjaxCall('','json',"http://localhost:8181/admin/last10query");           
        $scope.querylist = result;
        console.log($scope.querylist);

HTML code HTML代码

<tr ng-repeat="x in querylist">
   <td>{{x.query}}</td>
   <td>{{x.id}}</td>  
</tr> 

the JSON array JSON数组

[{"id":38,"query":"mm","temp_id":null,"topic_id":null},
{"id":37,"query":"n","temp_id":null,"topic_id":null},
{"id":36,"query":"nn","temp_id":null,"topic_id":null},
{"id":35,"query":"mm","temp_id":null,"topic_id":null},
{"id":34,"query":"nn","temp_id":null,"topic_id":null},
{"id":33,"query":"m","temp_id":null,"topic_id":null},
{"id":32,"query":"m","temp_id":null,"topic_id":null},
{"id":31,"query":"hi","temp_id":null,"topic_id":null},
{"id":30,"query":"j","temp_id":null,"topic_id":null},
{"id":29,"query":"h","temp_id":null,"topic_id":null}]

在此处输入图片说明

在此处输入图片说明

 angular.module("a",[]).controller("ac",function($scope){ $scope.querylist =[{"id":38,"query":"mm","temp_id":null,"topic_id":null}, {"id":37,"query":"n","temp_id":null,"topic_id":null}, {"id":36,"query":"nn","temp_id":null,"topic_id":null}, {"id":35,"query":"mm","temp_id":null,"topic_id":null}, {"id":34,"query":"nn","temp_id":null,"topic_id":null}, {"id":33,"query":"m","temp_id":null,"topic_id":null}, {"id":32,"query":"m","temp_id":null,"topic_id":null}, {"id":31,"query":"hi","temp_id":null,"topic_id":null}, {"id":30,"query":"j","temp_id":null,"topic_id":null}, {"id":29,"query":"h","temp_id":null,"topic_id":null}]; }); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="a" ng-controller="ac"> <table> <tr ng-repeat="x in querylist track by $index"> <td>{{x.query}}</td> <td>{{x.id}}</td> </tr> </table> </div> 

I don't know how you are getting the error. 我不知道您是怎么得到错误的。 See the snippet it is working for us. 查看它对我们有用的代码片段。

I got this kind of issues in Angular 2. So in this case you could initialize the querylist object globally. 我在Angular 2中遇到了这类问题。因此,在这种情况下,您可以全局初始化querylist对象。

please do this 请这样做

$scope.querylist =[]
 result = doAjaxCall('','json',"http://localhost:8181/admin/last10query");           
        $scope.querylist = result;
        console.log($scope.querylist);

If your json objects have unique Id, Then you can try track by id , 如果您的json对象具有唯一的ID,那么您可以尝试track by id

ex: 例如:

<tr ng-repeat="x in querylist track by x.id">
   <td>{{x.query}}</td>
   <td>{{x.id}}</td>  
</tr> 

I have solved the issue by using angular $http.get, i dont know why custom ajxa is not working. 我已经通过使用有角$ http.get解决了这个问题,我不知道为什么自定义ajxa无法正常工作。

    $http.get("http://localhost:8181/admin/last10query")
      .then(function(response) {
         $scope.myWelcome = response.data;
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM