Angularjs 使用Angular访问JSON中子嵌套对象中的值
我正在尝试访问结果的子数组对象中的值:Angularjs 使用Angular访问JSON中子嵌套对象中的值,angularjs,json,Angularjs,Json,我正在尝试访问结果的子数组对象中的值: results: [ { list_name: "E-Book Fiction", display_name: "E-Book Fiction", bestsellers_date: "2016-09-03", published_date: "2016-09-18", rank: 1, rank_last_week: 0, weeks_on_list: 1, asterisk: 0,
results: [
{
list_name: "E-Book Fiction",
display_name: "E-Book Fiction",
bestsellers_date: "2016-09-03",
published_date: "2016-09-18",
rank: 1,
rank_last_week: 0,
weeks_on_list: 1,
asterisk: 0,
dagger: 0,
amazon_product_url: "http://www.amazon.com/Great-Reckoning-Novel-Inspector-Gamache/dp/1250022134?tag=thenewyorktim-20",
isbns: [],
book_details: [
{
title: "A GREAT RECKONING",
description: "An instructor at the police academy is found murdered, perhaps by one of the cadets favored by Armand Gamache, the retired homicide chief of the Sûreté du Québec.",
contributor: "by Louise Penny",
author: "Louise Penny",
contributor_note: "",
price: 0,
age_group: "",
publisher: "Minotaur",
primary_isbn13: "9781250022127",
primary_isbn10: "1250022126"
}
],
reviews: [
{
book_review_link: "",
first_chapter_link: "",
sunday_review_link: "",
article_chapter_link: ""
}
]
}
因此,我可以从book_details中获取诸如title之类的值,但目前只能通过以下方式显示整个响应:
<article class="search-result row" ng-repeat="books in results">
<div class="col-xs-12 col-sm-12 col-md-3">
{{books}}
</div>
</article>
web控制台中对象的屏幕截图:
你可以这样写。通过迭代结果数组,在作用域中创建一个新的books数组
app.controller("MyController", ["$scope","$http",
function($scope, $http) {
$scope.books;
$http.get('test.json').then(function (response){
console.log(response.data.results);
$scope.results = response.data.results;
$scope.results.map(function(obj, index) {
if(obj.book_details) {
if(!($scope.books)) {
$scope.books = [];
}
for(var i in obj.book_details) {
$scope.books.push(obj.book_details[i]);
}
}
});
console.log($scope.books);
});
}]);
<article class="search-result row" ng-repeat="book in ">
<div class="col-xs-12 col-sm-12 col-md-3">
{{book.title}}
</div>
</article>
这没有任何影响。我应该能够输出标题:伟大的清算。我已经在我的网络控制台中为这个问题附加了一个对象的屏幕截图。从你的回答来看,结果是一个数组,该数组中是否有多个元素?问题是,你只能获取第一本书的标题。我需要能够迭代结果数组中的每个对象,以及book_details子数组,并获取每个标题。让我们来迭代book details,而不是结果。
app.controller("MyController", ["$scope","$http",
function($scope, $http) {
$scope.books;
$http.get('test.json').then(function (response){
console.log(response.data.results);
$scope.results = response.data.results;
$scope.results.map(function(obj, index) {
if(obj.book_details) {
if(!($scope.books)) {
$scope.books = [];
}
for(var i in obj.book_details) {
$scope.books.push(obj.book_details[i]);
}
}
});
console.log($scope.books);
});
}]);
<article class="search-result row" ng-repeat="book in ">
<div class="col-xs-12 col-sm-12 col-md-3">
{{book.title}}
</div>
</article>