0

I am working on an app, and part of it is supposed to list the names of people from a javascript object. However, it does not seem to iterate through my json as I would expect, instead it returns the entire thing as if it was one object. Why does it do this?

Broken Plunker https://plnkr.co/edit/u9DHsTlaO0H2G2jqnzLa

Index.html

<!DOCTYPE html>
<html ng-app="plunker">
  <head>
    <meta charset="utf-8" />
    <title>AngularJS Plunker</title>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.2/angular.js"></script>
    <script src="app.js"></script>
  </head>
  <body>
    <people-list></people-list>
  </body>
</html>

app.js

var app = angular.module("plunker", []);
app.directive("peopleList", function() {
    return {
      restrict: "E",
      templateUrl: "people-list.html",
      controller: function() {
        this.people = peopleObject;
      },
      controllerAs: "people"
    };
  });

   var peopleObject = [
    { firstname: 'John',
          middlename: 'K',
          lastname: 'Doe',
          sex: "M"
    }, 
      {
        firstname: 'Jane',
          middlename: 'R',
          lastname: 'Doe',
          sex: "F"
    }, 
      {   
        firstname: 'Jacob',
          middlename: 'Harold',
          lastname: 'Smith',
          sex: "M"
      }
  ];

people-list.html

<ul class="list-group menuIcons">
    <li ng-click="tab.setTab(1);" class="list-group-item" ng-repeat="person in people">
      {{person.firstname}}
    </li>
</ul>

3 Answers 3

4

Try change to this. you define controllerAs in directive then you should use that in template

<ul class="list-group menuIcons">
  <li ng-click="tab.setTab(1);" class="list-group-item" ng-repeat="person in   people.people">
  {{person.firstname}}
 </li>
</ul>
Sign up to request clarification or add additional context in comments.

3 Comments

Can I ask, why did the user use this. ? and when i tried removing it didnot even work. Why is that so?
That did it, thanks! I still don't understand where that extra 'people' came from though
@Smit user used controllerAs syntax.
0

people contain another object people, so you need to use person.person

like as below code

<ul class="list-group menuIcons">
    <li ng-click="tab.setTab(1);" class="list-group-item" ng-repeat="person in people">
      {{person.firstname}}
    </li>
</ul>

Check working plnkr

Comments

0

You missed out to put controllerAS alias in ng-repeat.

<ul class="list-group menuIcons">
    <li ng-click="tab.setTab(1);" class="list-group-item" ng-repeat="person in people.people">
      {{person.firstname}}
    </li>
</ul>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.