3

I need to use ng-repeat to produce several elements but those elements cannot be each wrapped inside a div (this is for box layout purposes where the box layout only works on the immediate children). For example, I need this result:

<div class='box-layout'>
  <div class='item-header>Head 1</div>
  <div class='item-body>Body 1</div>

  <div class='item-header>Head 2</div>
  <div class='item-body>Body 2</div>
</div>

There is no wrapping element on the repeated sections. This structure is required to make use of flex-box style layouts. How can I do this with AngularJS?

edA-qa mort-ora-y
  • 30,295
  • 39
  • 137
  • 267

2 Answers2

4

As @Anders Bornholm said, it can't be done (fyi, in knockout.js this is easy).

Albiet its ugly, you can accomplish this via a directive, e.g.:

  1. AngularJS ng-repeat with no html element
  2. ng-repeat without HTML element (this time really without any)

JS:

directive('htmlAppend', function() {
    return {
        restrict: 'A',
            link: function(scope, element, attrs) {
                element.append(attrs.htmlRepeat);
        }
    };
})

HTML:

<div class='box-layout'>
  <div class='item-header' ng-repeat="s in sections" html-append="<div class='item-body'>Body</div>">Head</div>
</div>
Community
  • 1
  • 1
Chris Montgomery
  • 2,344
  • 2
  • 19
  • 30
1

Short answer: can't be done. One of Angular's biggest drawbacks for me personally.

If directive templates would have allowed more than one root element you could have done it with a directive, but that doesn't work either.

Anders Bornholm
  • 1,326
  • 7
  • 18