使用Angular在JSON中访问子嵌套对象中的值

时间:2016-09-13 01:19:02

标签: angularjs json

我正在尝试访问结果的子数组对象中的值:

title

这样我就可以从book_details获取<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;
    });
});

我的控制器非常简单,只需抓住结果:

source venv/bin/activate

Web控制台中对象的屏幕截图:

https://docs.oracle.com/javase/7/docs/api/javax/swing/SwingUtilities.html#invokeLater(java.lang.Runnable)

1 个答案:

答案 0 :(得分:-1)

你可以这样写。通过遍历结果数组在作用域中创建新的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>

Working link