如何进行不重复ng-repeat元素的ng-repeat

时间:2013-04-12 13:44:53

标签: angularjs

我需要使用ng-repeat来生成多个元素,但这些元素不能分别包含在div中(这是用于框布局的,其中框布局仅适用于直接子元素)。例如,我需要这个结果:

<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>

重复部分没有包裹元素。需要此结构才能使用flex-box样式布局。我怎么能用AngularJS做到这一点?

2 个答案:

答案 0 :(得分:4)

正如@Anders博恩霍尔姆所说,无法做到(fyi,in knockout.js this is easy)。

Albiet它的丑陋,你可以通过一个指令完成这个,例如:

  1. AngularJS ng-repeat with no html element
  2. ng-repeat without HTML element (this time really without any)
  3. 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>
    

答案 1 :(得分:1)

简答:无法完成。对于我个人来说,Angular最大的缺点之一就是。

如果指令模板允许多个根元素,您可以使用指令完成它,但这也不起作用。

相关问题