Nested ng-repeat-start / ng-repeat-end

Nested ng-repeat-start / ng-repeat-end

本文关键字:ng-repeat-end ng-repeat-start Nested      更新时间:2023-09-26

ng-repeat-start/ng-repeat-end用于遍历数据而不呈现父元素[Portal to Docs]。在我的情况下,我想在没有额外元素的嵌套ng-repeat-start/ng-repeat-end块中显示数据。

我试图将两个ng-repeat-start指令放入同一元素,但它无法正确显示。我想出了一个变通办法,如下图所示。对于这种情况有更好的解决方案吗?

[{
    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
    }]
}]

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

您的内部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>

尝试在body标签内使用第一个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>