ng-repeat在JSON对象数组上显示“Repeater中的重复键”。 “追踪$ index”也无效。
角度代码
result = doAjaxCall('','json',"http://localhost:8181/admin/last10query");
$scope.querylist = result;
console.log($scope.querylist);
HTML代码
<tr ng-repeat="x in querylist">
<td>{{x.query}}</td>
<td>{{x.id}}</td>
</tr>
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}]
答案 0 :(得分:0)
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}];
});
&#13;
<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>
&#13;
我不知道你是如何得到这个错误的。查看它为我们工作的片段。
我在Angular 2中遇到了这类问题。因此,在这种情况下,您可以全局初始化querylist
对象。
请这样做
$scope.querylist =[]
result = doAjaxCall('','json',"http://localhost:8181/admin/last10query");
$scope.querylist = result;
console.log($scope.querylist);
答案 1 :(得分:0)
如果您的json对象具有唯一ID,那么您可以尝试track by id
,
例如:
<tr ng-repeat="x in querylist track by x.id">
<td>{{x.query}}</td>
<td>{{x.id}}</td>
</tr>
答案 2 :(得分:0)
我已经使用angular $ http.get解决了这个问题,我不知道为什么自定义ajxa无效。
$http.get("http://localhost:8181/admin/last10query")
.then(function(response) {
$scope.myWelcome = response.data;
});