0

EDIT: Sorry this is already asked - Adding rows with ng-repeat and nested loop

How do you do this in AngularJS?

foreach(var item in items) {                 <-- How to do this loop

  <tr><td>{{item.Name}}</td></tr>

  foreach(var subitem in item.subitems) {    <-- AND this loop?

    <tr class='sub'><td>{{subitem.Name}}</td></tr>

  }
}

Note that the <tr> element is not nested.

A possible expected result:

<tr><td>Item 1</td></tr>
<tr class='sub'><td>Item 1.1</td></tr>
<tr class='sub'><td>Item 1.2</td></tr>
<tr><td>Item 2</td></tr>
4
  • I'd create an array containing all the rows using JavaScript code in the controller, and use a single ng-repeat in the view. Commented Apr 27, 2015 at 6:42
  • 1
    possible duplicate of Adding rows with ng-repeat and nested loop Commented Apr 27, 2015 at 6:46
  • why you dont think of ng-repeat to use here Commented Apr 27, 2015 at 6:47
  • I would follow this stackoverflow answer: stackoverflow.com/questions/15362868/… Commented Apr 2, 2020 at 15:47

1 Answer 1

0

You could achieve this using tbody element as the first ng-repeat container, like this:

<table>
  <tbody ng-repeat="item in items">
    <tr><td>{{item.Name}}</td></tr>
    <tr class="sub" ng-repeat="subitem in item.subitems">
      <td>{{subitem.Name}}</td>
    </tr>
  </tbody>
</table>
Sign up to request clarification or add additional context in comments.

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.