1

I'm having trouble getting an object's child array to be displayed with AngularJS.

In my app.js, I'm defining an array of objects as my main scope.

function MainCtrl($scope) {
    $scope.products = [
    {
        "id": "1",
        "title": "Red Hat",
        "sku": "Hat",
        "thumb": "100x100",
        "pages": [
            {
                "id": "360",
                "imgs": "01",
                "content": "stuff"
            },
            {
                "id": "Feature",
                "imgs": "02",
                "content": "Feature stuff"
            },
            {
                "id": "Size",
                "imgs": "03",
                "content": "Data stuff"
            }
        ]
    },
    {
        "id": "2",
        "title": "Blue Hat",
        "sku": "Hat",
        "thumb": "100x100",
        "pages": [
            {
                "id": "360",
                "imgs": "01",
                "content": "stuff"
            },
            {
                "id": "Feature",
                "imgs": "02",
                "content": "Feature stuff"
            },
            {
                "id": "Size",
                "imgs": "03",
                "content": "Data stuff"
            }
        ]
    }
]}

When I try to display the title, sku and thumbnail, everything renders correctly. My issue is when I try to display the pages array. When I try to make a list of the pages and just get their id's, nothing is even rendered.

  <ul ng-repeat="page in product.pages">
      <li>{{pages.page.id}}</li>
        <li>{{page.content}}</li>
    </ul>

I'm still new to angular and I'm not certain on the correct way to do things. So where am I going wrong with my code?

I put together a plunker with my code. Any help or pointers in the right direction is appreciated.

0

1 Answer 1

4

Your issue is that your ng-repeat for page in product.pages is outside of your ng-repeat for product in products.

Your html should look like this:

<ul ng-repeat="product in products">
  <li>{{product.sku}}</li>
  <li>{{product.title}}</li>
  <li>{{product.thumb}}</li>
  <ul ng-repeat="page in product.pages">
    <li>{{page.id}}</li>
    <li>{{page.content}}</li>
  </ul>
</ul>

Here's a working Plunker.

1
  • No problem! Glad to have helped. Commented Jan 15, 2014 at 4:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.