我知道这有点基本,但我正在努力理解它,我有一个返回以下 JSON 的 Web 服务:
[{"search_id":"1","user_id":"1","all_words":"php","not_words":"C++","one_words":"java","created_at":null,"updated_at":null,"search_name":null},{"search_id":"2","user_id":"1","all_words":"second","not_words":"not","one_words":"one","created_at":null,"updated_at":null,"search_name":null}]
因此,当它变得有 Angular 时,我最终得到以下结果:
Array[2]
0: Object
$$hashKey: "object:5"
all_words: "php"
created_at: null
not_words: "C++"
one_words: "java"
search_id: "1"
search_name: null
updated_at: null
user_id: "1"
__proto__:
1: Object
$$hashKey: "object:6"
all_words: "second"
created_at: null
not_words: "not"
one_words: "one"
search_id: "2"
search_name: null
updated_at: null
user_id: "1"
__proto__:
在 ng-repeat 中使用这确实很痛苦,我该如何才能像这样访问它(粗略的例子)
ng-repeat="item in items"
{{ item.search_id }}
要明确的是,我可以从中获取数据的唯一方法是:
<tr ng-repeat="items in data">
<td ng-repeat="(key, value) in items"> </td>
</tr>
Controller 代码在这里:
testAPI.getSearches().then(function (data) {
$scope.data= testAPI.searchList();
console.log($scope.data);
}, function (error) {
alert("Error in getSearches");
});
getsearches如下,searchList返回searchs变量:
getSearches: function() {
var deferred = $q.defer();
$http({
url: 'http://localhost/api/api/tray/search/list'
}).success(function (data) {
searches = data;
console.log(data);
deferred.resolve(data);
}).error(function (data) {
alert('Error');
deferred.reject(data);
});
return deferred.promise;
},
嗯,感谢大家的回复,但简单的“数据中的项目”在我的情况下不起作用,我必须在嵌套 ng 重复内的项目中使用(键,值),我缺少什么想法吗?
顺便说一句,不确定这是否重要,将 HTML 放在部分内容中,并且我正在使用 ui 路由器进行导航?
更新 非常感谢大家的帮助,看起来这个问题是由包含 HTML 元素的拼写错误引起的,并且 Controller 设置不正确,因为我弄乱了 ui 路由器设置。一旦我有机会确保我没有错过任何其他内容,我就会回复。
最佳答案
这似乎工作正常。只需确保您的数据与您的 $scope 相关联。 http://jsfiddle.net/f4zdfh72/
function MyCtrl($scope) {
$scope.data = [{
"search_id": "1",
"user_id": "1",
"all_words": "php",
"not_words": "C++",
"one_words": "java",
"created_at": null,
"updated_at": null,
"search_name": null
}, {
"search_id": "2",
"user_id": "1",
"all_words": "second",
"not_words": "not",
"one_words": "one",
"created_at": null,
"updated_at": null,
"search_name": null
}]
}
<div ng-controller="MyCtrl">
<div ng-repeat='item in data'>
THIS IS DATA: {{item.one_words}}
</div>
</div>
注意:在 JSFiddle 中运行它。堆栈片段仅用于代码预览
关于javascript - AngularJS 多维 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27534025/