4

I am looking for a way to bind data from a three dimensional array in the scope with my view..

My scope (short version) looks like this

$scope.publicationData = [ 
    { pubId:'1' , pubName:'Level 1 A' , pubCat:[
        { pubCatId:'1' , pubCatName:'Level 2 A', pubSubCat:[ 
            { pubSubCatId:'1' , pubSubCatName:'Level 3 A' }, 
            { pubSubCatId:'2' , pubSubCatName:'Level 3 B' }
            ]
        },
        { pubCatId:'2' , pubCatName:'Level 2 B', pubSubCat:[
            { pubSubCatId:'3' , pubSubCatName:'Level 3 C' },
            { pubSubCatId:'4' , pubSubCatName:'Level 3 D' }
            ]
        }
        ]
    }];

In the view I have code that successfully presents what is in the first and second array dimension,, but I can't get values from pubSubCatId or pubSubCatName

HTML + Angular View

<div ng-controller="myPublictionCtrl">

<div ng-repeat="publication in publicationData">

    <ul>
        <li >
                            <!-- This works -->
            {{publication.pubId}}. {{publication.pubName}}
        </li>
    </ul>

    <ul>
                    <!-- This works -->

        <li ng-repeat="category in publication.pubCat">
            {{category.pubCatId}}. {{category.pubCatName}}
        </li>

    </ul>

    <ul>
                    <!-- This doesn't work -->

        <li ng-repeat="subcategory in publication.pubCat.pubSubCat">
            {{subcategory.pubSubCatName}}
        </li>
    </ul>
</div>
</div>

How would I retrieve data from deeper layers of the scope. Can AngularJS do this?

3
  • This doesn't look right publication.pubCat.pubSubCat... pubCat is an array. Commented Nov 11, 2013 at 22:25
  • How you want to present it? You could use nested ng-repeats, if you wanted to. Commented Nov 11, 2013 at 22:32
  • I don't really want a nested presentation... but it may be the only way I can do it.. Commented Nov 11, 2013 at 22:41

2 Answers 2

10

The third loop must be nested inside the second one, and use

subcategory in category.pubSubCat
Sign up to request clarification or add additional context in comments.

1 Comment

Oh Thanks! I originally used category.pubSubCat and I couldn't get it to display data so it threw me off and I started doing illogical things.. I ended up having the nesting issue. Thanks.
2
<div ng-controller="myPublictionCtrl">
        <div ng-repeat="publication in publicationData">
            <ul>
                <li style="text-align:left">                       
                    {{publication.pubId}}. {{publication.pubName}}
                </li>
            </ul>
            <ul>
                <li ng-repeat="category in publication.pubCat" style="text-    align:left">{{category.pubCatId}}. {{category.pubCatName}}               
                <ul> 
                <li ng-repeat="subcategory in category.pubSubCat" style="text-align:left">{{category.pubCatId}}.{{subcategory.pubSubCatId}}.     {{subcategory.pubSubCatName}}
                </li>
                </li>
                </ul>
            </ul>
        </div>
    </div>

1 Comment

It'd be great if you could add a little explanation to your code.

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.