嵌套的ng-repeat-start / ng-repeat-end

时间:2014-08-13 14:20:40

标签: javascript angularjs

ng-repeat-start / ng-repeat-end用于循环数据而不渲染父元素[Portal to Docs]。就我而言,我想在嵌套的ng-repeat-start / ng-repeat-end块中显示数据,而无需额外的元素。

我试图将两个ng-repeat-start指令放入同一个元素中,但无法正确显示。我以某种方式想出了[Portal to Demo]下面显示的解决方法。对于像这样的案例有更好的解决方案吗?

数据

[{
    name: 'Chapter 1',
    sections: [{
        name: '1-1',
        words: 1024
    }, {
        name: '1-2',
        words: 512
    }]
}, {
    name: 'Chapter 2',
    sections: [{
        name: '2-1',
        words: 2048
    }, {
        name: '2-2',
        words: 256
    }]
}]

HTML

<tr class="tr-for" ng-repeat-start="chapter in main.book"></tr>
  <tr ng-repeat-start="section in chapter.sections" ng-repeat-end>
    <td>{{chapter.name}}</td>
    <td>{{section.name}}</td>
    <td>{{section.words}}</td>
  </tr>
<tr class="tr-end" ng-repeat-end></tr>

CSS

tr.tr-for,
tr.tr-end {
  display: none;
}

1 个答案:

答案 0 :(得分:3)

你的内部ng-repeat应该是常规的ngRepeat:

<tr class="tr-for" ng-repeat-start="chapter in main.book"></tr>
    <tr ng-repeat="section in chapter.sections">
        <td>{{chapter.name}}</td>
        <td>{{section.name}}</td>
        <td>{{section.words}}</td>
    </tr>
<tr class="tr-end" ng-repeat-end></tr>

<强>更新

尝试使用tbody标签内的第一个ngRepeat:

<tbody ng-repeat="chapter in main.book">
  <tr ng-repeat-start="section in chapter.sections" ng-repeat-end>
    <td>{{chapter.name}}</td>
    <td>{{section.name}}</td>
    <td>{{section.words}}</td>
  </tr>
</tbody>