Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have the following controller:

controller('homepageCtrl', function ($scope) {
    $scope.menu = {
      "Home": "/",
      "Data Sources": {
        "Add": "/datasources/add",
        "Edit": "/datasources/edit",
        "List": "/datasources/list"
      },
      "Test": "/test"
    }
});

And the following HTML (JADE)

div(ng-controller='homepageCtrl')
        div(ng-repeat='(key, val) in menu')
          {{key}} - {{val}}
          div(ng-repeat='(k, v) in val')
            {{k}} - {{v}}

I want to loop through the JSON object but also to check if the current value is another JSON object and if it is then to iterate through it as well.

The code above currently generates:

Data Sources - {"Add":"/datasources/add","Edit":"/datasources/edit","List":"/datasources/list"}
Add - /datasources/add
Edit - /datasources/edit
List - /datasources/list
Home - /
0 - /
Test - /test

However I want to it generate:

Add - /datasources/add
Edit - /datasources/edit
List - /datasources/list
Home - /
0 - /
Test - /test
share|improve this question
    
Your HTML is not HTML. –  artur grzesiak Nov 27 '13 at 9:29
    
@arturgrzesiak It's jade, sorry I'll update the question –  hoverhand Nov 27 '13 at 9:30
add comment

1 Answer

Why not just using ng-if:

<div ng-repeat="(key, val) in menu">
    <div ng-repeat="(k, v) in val" ng-if="typeof val == 'object'">
        ...
    </div>
</div>
share|improve this answer
    
Thanks @Moritz, but val is a json element not an Array –  hoverhand Nov 27 '13 at 9:46
    
I have updated the example to check the json Object. –  Moritz Petersen Nov 28 '13 at 9:42
add comment

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.