2016-09-13 53 views
1

我想在結果子數組對象的訪問值:在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, 
    dagger: 0, 
    amazon_product_url: "http://rads.stackoverflow.com/amzn/click/1250022134", 
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> 

我的控制是相當簡單,只要抓住了結果:

myBooks.controller('bookController', function($scope, $http){ 
    $http.get("data-url") 
    .success(function(response) { 
     console.log(response); 
     $scope.results = response; 
    }); 
}); 

在Web控制檯對象的屏幕截圖:

enter image description here

+0

迭代過書的細節不是結果。 –

回答

-1

你可以把它寫這樣。通過迭代結果數組來創建新的書籍數組。

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> 

Working link

+0

這沒有任何影響。 – user3438917

+0

你想要在視圖中顯示什麼? –

+0

我應該可以輸出標題:偉大的推算。我在我的Web控制檯中附加了該對象的屏幕截圖。 – user3438917