Bernard Parah Bernard Parah - 4 months ago 9
AngularJS Question

Access Json data in angular js

What will be the proper way to access the nested elements of this json file in angular?

Using $http in controller

$http.get('app/api.json')
.success(function(data){
$scope.feeds = data;
});


api.json

{
"feed": {
"data": [{
"story": "test story",
"updated_time": "2016-06-05T17:17:49+0000",
"id": "1"
}, {
"message": "jkhkjhkhjlkhk",
"updated_time": "2015-05-08T07:23:14+0000",
"id": "2"
}

],
}
}


the view

<div ng-repeat="feed in feeds">{{feed.data.id}}</div>


Right now I am more concerned with accessing the id's which are common, I see trouble when coming to display the story or messages field since they are different.

PS: I am getting info from a facebook group through the graph api

Answer

As commented, feed.data is an array and you will have to loop over it.

Sample

function myCtrl($scope) {
  $scope.d = {
    "feed": {
      "data": [{
          "story": "test story",
          "updated_time": "2016-06-05T17:17:49+0000",
          "id": "1"
        }, {
          "message": "jkhkjhkhjlkhk",
          "updated_time": "2015-05-08T07:23:14+0000",
          "id": "2"
        }

      ],
    }
  }

}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.10/angular.min.js"></script>
<div ng-app>
  <div ng-controller="myCtrl">
    <ul ng-repeat="o in d.feed.data">
      <li>{{o.id}}</li>
    </ul>
  </div>
</div>