MuddyMudSkipper
MuddyMudSkipper

Reputation: 79

How can I display data from nested JSON in the context of the ng-click (Angular, JSON)

I'm trying to figure out how I can display nested json data that pertains to the data from the previous ng-click.

Any insight would be greatly appreciated, thanks!

plunker: http://plnkr.co/edit/C7MlbHDawSdASHl1aLBB?p=preview

HTML

<ul class="list-unstyled">
  <li ng-repeat="content in contentSets" ng-click="setContent(content)">
    <a href="#">{{content.name}}</a>
  </li>
</ul>

<h1>{{useContent.contentTitle}}</h1>

<ul class="list-unstyled">
  <li ng-repeat="headline in useContent.headlines">
    {{headline.title}}
  </li>
</ul>

JSON

            {
              "contentData": [

                {
                    "contentId": 1,
                    "name": "Content set 1",
                    "contentTitle": "Pretaining to content set 1",
                    "news": [
                        {
                          "headlineSet": 1,
                          "headlines": [
                              {
                                "title": "headline 1 (set 1)",
                                "date": "date for headline 1 (set 1)"
                              },
                              {
                                "title": "headline 2 (set 1)",
                                "date": "date for headline 2 (set 1)"
                              },
                              {
                                "title": "headline 3 (set 1)",
                                "date": "date for headline 3 (set 1)"
                              }
                            ]
                        }
                    ]

                },
                {
                    "contentId": 2,
                    "name": "Content set 2",
                    "contentTitle": "Pretaining to content set 2",
                    "news": [
                        {
                          "headlineSet": 1,
                          "headlines": [
                              {
                                "title": "headline 1 (set 2)",
                                "date": "date for headline 1 (set 2)"
                              },
                              {
                                "title": "headline 2 (set 2)",
                                "date": "date for headline 2 (set 2)"
                              },
                              {
                                "title": "headline 3 (set 2)",
                                "date": "date for headline 3 (set 2)"
                              }
                            ]
                        }
                    ]
                },
                {
                    "contentId": 3,
                    "name": "Content set 3",
                    "contentTitle": "Pretaining to content set 3",
                    "news": [
                        {
                          "headlineSet": 1,
                          "headlines": [
                              {
                                "title": "headline 1 (set 3)",
                                "date": "date for headline 1 (set 3)"
                              },
                              {
                                "title": "headline 2 (set 3)",
                                "date": "date for headline 2 (set 3)"
                              },
                              {
                                "title": "headline 3 (set 3)",
                                "date": "date for headline 3 (set 3)"
                              }
                            ]
                        }
                    ]
                }
              ]
            }

JS

 function AppController($scope, $http) {

   $http.get('content.json').success(function(data) {
     $scope.contentSets = data.contentData;

     $scope.news = [];
     $scope.headlines = [];

     angular.forEach(data.contentData, function(demoContent, index){

      angular.forEach(demoContent.news, function(newsGroup, index){
       $scope.news.push(newsGroup);

       angular.forEach(newsGroup.headlines, function(headline, index){
        $scope.headlines.push(headline);
       });
     });
    });

  });

  $scope.setContent = function(content) {
    $scope.useContent = content;
  }
}

Upvotes: 2

Views: 2714

Answers (1)

Bogdan Savluk
Bogdan Savluk

Reputation: 6312

You can do it directly in view:

<ul class="list-unstyled" ng-repeat="news in useContent.news">
  <li ng-repeat="headline in news.headlines">
    {{headline.title}}
  </li>
</ul>

http://plnkr.co/edit/WoNBqN13aRlN3eUwjh3t

Upvotes: 2

Related Questions