Dismiss
Announcing Stack Overflow Documentation

We started with Q&A. Technical documentation is next, and we need your help.

Whether you're a beginner or an experienced developer, you can contribute.

Sign up and start helping → Learn more about Documentation →

I am trying to get the JSON data into list.html as follows, but my attempts are failing. I have tried following patterns described in other similar posts, but haven't had any luck and haven't found a scenario with JSON data formatted exactly like mine. How do I get the fields like givenName, familyName, primaryTitle, phones[0].value, and photo.small to show up?

index.html:

<!doctype html>
<html lang="en" ng-app="directoryApp">
<head>
  <meta charset="UTF-8">
  <title>Directory</title>
  <script src="lib/angular/angular.min.js"></script>
  <script src="lib/angular/angular-route.min.js"></script>
  <script src="lib/angular/angular-animate.min.js"></script>

  <script src="js/app.js"></script>
  <script src="js/controllers.js"></script>
  <link rel="stylesheet" href="css/style.css">
</head>
<body>
<div ng-view></div>
</body>
</html>

list.html:

<ul ng-show="query">
    <li ng-animate="'animate'" ng-repeat="person in people.people | filter: query | orderBy: peopleOrder:direction">
        <img ng-src="images/{{person.photo.small}}" alt="Photo of {{person.name.givenName}}">
        <div class="info">
          <h2>{{person.name.givenName}}</h2>
          <h3>{{person.primaryTitle}}, {{person.phones[0].value}}</h3>
        </div>
    </li>
  </ul>

controllers.js:

var directoryControllers = angular.module('directoryControllers', ['ngAnimate']);

directoryControllers.controller('ListController', ['$scope', '$http', function($scope, $http) {
  $http.get('js/directoryData.json').success(function(data) {
    $scope.people = data;
    $scope.peopleOrder = 'familyName';
  });
}]);

app.js:

var directoryApp = angular.module('directoryApp', [
  'ngRoute',
  'directoryControllers'
]);

directoryApp.config(['$routeProvider', function($routeProvider) {
  $routeProvider.
  when('/list', {
    templateUrl: 'partials/list.html',
    controller: 'ListController'
  }).
  otherwise({
    redirectTo: '/list'
  });
}]);

directoryData.json:

[
    {
        "uid": 15, 
        "name": "School of Programming", 
        "sortKey": 0, 
        "type": "Academic", 
        "address": {
            "address1": "255 Foo St",
            "address2": "Suite 310",
            "city": "FooBar",
            "state": "FB",
            "postalCode": "90210"
        },
        "phones": [
            {
                "type": "Work", 
                "value": "555-1616"
            },

            {
                "type": "Fax", 
                "value": "555-3620"
            }
        ],
        "people": [
            {
                "uid": "person1", 
                "classification": "Faculty", 
                "name": {
                    "givenName": "Roo",
                    "nickname": "",
                    "additionalName": "",
                    "familyName": "Baf"
                },
                "primaryTitle": "Part Time Worker",
                "phones": [
                    {
                        "type": "Work", 
                        "value": "555-1616"
                    },

                    {
                        "type": "Mobile", 
                        "value": "555-1509"
                    }
                ],
                "photo": {
                    "small": "/__media__/photos/foo_portrait_small.jpg", 
                    "medium": "/__media__/photos/foo_portrait_medium.jpg", 
                    "large": "/__media__/photos/foo_portrait_large.jpg"
                }
            },

            {
                "uid": "person2", 
                "classification": "Faculty", 
                "name": {
                    "givenName": "Foo",
                    "nickname": "",
                    "additionalName": "P.",
                    "familyName": "Bar"
                },
                "primaryTitle": "Blah",
                "phones": [
                    {
                        "type": "Work", 
                        "value": "555-3608"
                    },

                    {
                        "type": "Home", 
                        "value": "555-4716"
                    }
                ],
                "photo": {
                    "small": "/__media__/photos/portrait_small.jpg", 
                    "medium": "/__media__/photos/portrait_medium.jpg", 
                    "large": "/__media__/photos/portrait_large.jpg" 
                }
            }
        ]
    },
    {
        "uid": 16, 
        "name": "School of Coding", 
        "sortKey": 1, 
        "type": "Academic", 
        "address": {
            "address1": "256 Foo St",
            "address2": "Suite 311",
            "city": "FooBar",
            "state": "FB",
            "postalCode": "90210"
        },
        "phones": [
            {
                "type": "Work", 
                "value": "555-1616"
            },

            {
                "type": "Fax", 
                "value": "555-3620"
            }
        ],
        "people": [
            {
                "uid": "person3", 
                "classification": "Faculty", 
                "name": {
                    "givenName": "Boo",
                    "nickname": "",
                    "additionalName": "",
                    "familyName": "Far"
                },
                "primaryTitle": "Part Time Worker",
                "phones": [
                    {
                        "type": "Work", 
                        "value": "555-1617"
                    },

                    {
                        "type": "Mobile", 
                        "value": "555-1508"
                    }
                ],
                "photo": {
                    "small": "/__media__/photos/fooz_portrait_small.jpg", 
                    "medium": "/__media__/photos/fooz_portrait_medium.jpg", 
                    "large": "/__media__/photos/fooz_portrait_large.jpg"
                }
            },

            {
                "uid": "person4", 
                "classification": "Faculty", 
                "name": {
                    "givenName": "Too",
                    "nickname": "",
                    "additionalName": "P.",
                    "familyName": "Mar"
                },
                "primaryTitle": "Blah",
                "phones": [
                    {
                        "type": "Work", 
                        "value": "555-3607"
                    },

                    {
                        "type": "Home", 
                        "value": "555-4717"
                    }
                ],
                "photo": {
                    "small": "/__media__/photos/Xportrait_small.jpg", 
                    "medium": "/__media__/photos/Xportrait_medium.jpg", 
                    "large": "/__media__/photos/Xportrait_large.jpg" 
                }
            }
        ]
    }
]
share|improve this question
    
Tried to add "$scope.people = {};" before $http.get? – Skarllot Oct 1 '15 at 13:59
1  
people.people which you are accessing in your ng-repeat is not defined, since ($scope).people is an array of objects. – muenchdo Oct 1 '15 at 14:01
up vote 1 down vote accepted

You have an array of objects that each have a people property in them and that property value is an array.

Thus you need to use nested ng-repeat to first loop over the main array and within each iteration of that repeat, loop over the inner array

<ul ng-repeat="item in people"> 
   <li ng-animate="'animate'" ng-repeat="person in item.people | filter: ...">
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.