5

I'm in the process of learning AngularJS. I would like to print out a list of objects and iterate over one of the object's inner object's properties. This looked like a standard procedure of using nested loops, however, it doesn't appear to be so simple.

My Controller is setup below. Essentially, it is a list of random vehicles.

var vehicleApp = angular.module("vehicleApp", []);

vehicleApp.controller('VehicleController', function ($scope) {
    $scope.vehicles = [{
        id: 0,
        name: "car",
        parts: {
            wheels: 4,
            doors: 4
        }
    }, {
        id: 1,
        name: "plane",
        parts: {
            wings: 2,
            doors: 2
        }
    }, {
        id: 2,
        name: "boat",
        parts: {
            doors: 1
        }
    }];
});

I'd like to output the vehicles as such:

car
 - wheels (4)
 - doors (2)

plane
 - wings (2)
 - doors (2)

boat
 - doors (1)

My template that I used was setup as such:

<div ng-app="vehicleApp" ng-controller="VehicleController">
    <p ng-repeat="vehicle in vehicles">
        {{ vehicle.name }}
    </p>
    <ul>
        <li ng-repeat="(attribute, value) in vehicle.parts">
            {{attribute}} ({{value}})
        </li>
    </ul>
</div>

This produces a list of the vehicles, but not the sub lists of the parts inner object.

Interestingly, enough, when I use {{ vehicle.parts }} it returns a JSON string of the parts inner object. Does AngularJS treat it as a string and hence, it is unable to print out the properties of the parts object?

Pete
  • 3,246
  • 4
  • 24
  • 43

1 Answers1

13

You didn't enclose the second ngRepeat in the first one:

<div ng-app="vehicleApp" ng-controller="VehicleController">
    <p ng-repeat="vehicle in vehicles">
        {{ vehicle.name }}
        <ul>
            <li ng-repeat="(attribute, value) in vehicle.parts">
                {{attribute}} ({{value}})
            </li>
        </ul>
    </p>
</div>
Blackhole
  • 20,129
  • 7
  • 70
  • 68
  • Changing this doesn't change the output. However, it did work when I changed the

    to a

    . I guess it requires that the HTML is well-formed?
    – Pete Dec 16 '13 at 21:29
  • @user2708395 Yes, HTML must be well-formed, naturally. Can you make a fiddle which shows the problem? – Blackhole Dec 16 '13 at 21:31
  • http://jsfiddle.net/Y3GLa/4/ - here is the fiddle with the

    as a wrapper. Changing it to a div makes it work. Thanks for the quick answer!

    – Pete Dec 16 '13 at 21:33
  • @user2708395 Well, that's really strange… I can't say where is the problem, sorry. – Blackhole Dec 16 '13 at 21:43
  • Could the problem be related to HTML validation where block elements can only contain inline elements? If I change the list to use 's instead, it works again. – Pete Dec 16 '13 at 21:52
  • I think you are right @user2708395 . because some one else faced a similar problem .see comments in http://stackoverflow.com/questions/19839743/angularjs-nested-ng-repeat – Harish Kayarohanam Dec 17 '13 at 04:47
  • the browser internally does arranging and gets block
      out of the p tag as a p tag cannot contain block level elements . so as it was taken out vehicle.parts does not make sense, so repeat does not work . do inspect element in the fiddle with p tag to see what I am saying .
      – Harish Kayarohanam Dec 17 '13 at 05:12