15

I am trying to iterate over a JSON object userTypes.
For the below code:

In the 1st ng-repeat:
{{user.type}} outputs 'Parent'(as expected),
{{user.options}} outputs '[{"option1":"11QWERT","option2":"22QWERT"}]'(as expected).

But in the 2nd ng-repeat, I am not able to iterate through the user.options and output each of the {{options}}

What should be changed to get the option1 and option2 as the outputs in 2nd ng-repeat ?

JS snippet

var userTypes = [{
    "type": 'Parent',
    "options": [{
        "option1": "11QWERT",
        "option2": "22QWERT"
    }]
}]

HTML snippet

<li ng-repeat="user in userTypes">
    <p>{{user.type}}</p>
    <p>{{user.options}}</p>
    <li ng-repeat="option in user.options">
        <p>
            {{option}}
        </p>
    </li>
</li>

6 Answers 6

11

Replace your child <li> with <ul> and then you can iterate user.options like so:

<li ng-repeat="user in userTypes">
    <p>{{user.type}}</p>
    <ul ng-repeat="(key, value) in user.options[0]">
        <p>{{key}}: {{value}}</p>
    </ul>
</li>

Or if your options may include more then one object:

<li ng-repeat="user in userTypes">
    <p>{{user.type}}</p>
    <ul ng-repeat="option in user.options">
        <li ng-repeat="(key, value) in option">{{key}}: {{value}}</li>
    </ul>
</li>

If you don't need object keys:

<ul ng-repeat="option in user.options">
    <li ng-repeat="item in option">{{item}}</li>
</ul>

Fiddle

Extended explanation:

In your example you have <li> tag inside another <li>:

<li ng-repeat="user in userTypes">
    <li ng-repeat="option in user.options">
    </li>
</li>

Since it is not a valid HTML browser will interprets this markup to following:

<li ng-repeat="user in userTypes">
</li>
<li ng-repeat="option in user.options">
</li>

Since ng-repeat creates new scope for each iteration you can't access user variable in second ng-repeat and iterator wouldn't run.

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

1 Comment

@dreamer: because it is a valid HTML, otherwise browser will close first <li> tag automatically and you will lose your nested iteration
1

For this exact JSON input it should be like this:

<li ng-repeat="option in user.options">
    <p>
        {{option.option1}}
        {{option.option2}}
    </p>
</li>

However as you said you want non fixed number of options, update your JSON to be like this:

"options":["11QWERT","22QWERT"]

And then your code should work as you wanted it.

You can add each new element to list with simple coma before it.

2 Comments

the number of options in options is not fixed. so this cannot be used?
Your JSON input is bad then, it should be: "options":["11QWERT","22QWERT"] ill update answer.
1

Since user.options is an array you should loop it again. By doing that you will get an object, with that object you can access your options1 and option2 easily.

please refer working plunker:

http://embed.plnkr.co/5c3i5fY50jLP0fFgxkUX/preview

see through the code if you have any doubt.

Hope this helps

1 Comment

no , this cannot be used because, the number of options in user.options will vary, hence i cannot call it explicitly using options1 and options2
1

A little aside, but just found out you can render a valid list HTML using ng-repeat-start/end in combination with ng-if="false" for the given array of arrays to flatten them:

<ul>
  <script ng-repeat-start="user in userTypes" ng-if="false"></script>
  <li ng-repeat="item in user.options">{{item}}</li>
  <script ng-repeat-end="" ng-if="false"></script>
</ul>

Comments

0

Just access the propertie of the option object in the second ng-repeat. Like option.option1

2 Comments

the number of options in options is not fixed. so this cannot be used
This solution doesn't depends on the length of the options collection. I guess your structure should be reworked.
0

You should probably make your user.options an object and not an array containing a single object.

Note the difference between these:

[{"option1":"11QWERT","option2":"22QWERT"}]
 {"option1":"11QWERT","option2":"22QWERT"}

You can then iterate the options with an ng-repeat like discussed here:

<li ng-repeat="(key, value) in user.options">
    <p>{{ key }}: {{ value }}</p>
</li>

Comments

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.