0

i have a data model which is an array having some object. these object may contain some child objects.so how to run ng-repeat on both these in one ng-repeat attribue.. here is an examle JSON

[
    {
        "foo": "bar",
        "values": null,
        "children": [
            {
                "foo": "bar",
                "values": null,
                "children": null
            }
        ]
    },
    {
        "foo": "bar",
        "children": [
            {
                "id": 1,
                "ques": "something"
            }
        ]
    }
]

4 Answers4

0

For example:

<div ng-repeat="object in objects">
  <div ng-repeat="child in object">
    {{child}}
  </div>
</div>

where objects is your main object you receive from for example http request, and child is a child within an object. You can go as deep in the child's of object as many child's your objects have.

In your case:

<div ng-repeat="object in objects">
   {{object.foo}} {{object.values}}
  <div ng-repeat="child in object">
    {{child.id}} {{child.ques}}
  </div>
</div>
uksz
  • 18,239
  • 30
  • 94
  • 161
0

Use your preferred collection-manipulation library to convert that to an array of the children.

For example, in lodash: _.flatten(_.pluck(yourArray, 'children')) (I think there's a chaining version of that too?)

Then just iterate over it normally.

The Manipulation could be done in the controller to set a variable on scope, or as a function defined on scope, or done inline in html.

Brondahl
  • 7,402
  • 5
  • 45
  • 74
0

Maybe templates with ng-include are what you are looking for. Here is a Plunker example with your given data.

AntiHeadshot
  • 1,130
  • 9
  • 24
0

You will need to use ng-repeat-start and ng-repeat-end.

ng-repeat-start is the same as ng-repeat however it allows us to output tags up to a ng-repeat-end which don't have to be child tags of the ng-repeat-start. You could then access the children outside of the ng-repeat-start tag if you like.

With your example you could do something like this...

            <tbody>
                <tr ng-repeat-start="parent in parents track by $index">
                    <td>Parent</td>
                    <td>{{parent.foo}}</td>
                </tr>
                <tr ng-repeat-end ng-repeat="child in parent.children track by $index">
                    <td>Child</td>
                    <td>{{child.foo}}</td>
                </tr>
            </tbody>
Hoots
  • 1,876
  • 14
  • 23