ng-repeat没有按预期工作

时间:2015-02-18 07:13:54

标签: angularjs angularjs-ng-repeat

下面的对象应该为一个表下的父对象和子对象生成表行(作为兄弟)。它怎么样,我用ng-repeat-start但没用。我需要它来进行相同的对齐。

[
{'name':'parent1','children':[{'name':'child1'},{'name':'child2'}]},
{'name':'parent2','children':[{'name':'child1'},{'name':'child2'}]}
......
]
<table>
<tr ng-repeat="parent in obj">
<td>{{parent.name}}</td>
</tr>
<tr ng-repeat="child in parent.children">
<td>{{child.name}}</td>
</tr>
...........

表格应如下所示:

<table>
<tr>
<td>Parent1</td>
</tr>
<tr>
<td>child1</td>
</tr>
<tr>
<td>child2</td>
</tr>
<tr>
<td>Parent2</td>
</tr>
<tr>
<td>child1</td>
</tr>
<tr>
<td>child2</td>
</tr>
......
</table>

2 个答案:

答案 0 :(得分:2)

您可以尝试

<table>
    <tr ng-repeat-start="parent in obj">
        <td>{{parent.name}}</td>
    </tr>
    <tr ng-repeat="child in parent.children">
        <td>{{child.name}}</td>
    </tr>
    <tr ng-repeat-end="">
    <tr/>
</table>

答案 1 :(得分:1)

一切都好,因为这段代码

<tr ng-repeat="child in parent.children">
<td>{{child.name}}</td>
</tr>

超出此范围

<tr ng-repeat="parent in obj">
<td>{{parent.name}}</td>
</tr>

如果你想达到你想要的效果,可以制作像这样的平面阵列

[
{'name':'parent1'},
{'name':'children1'},
...
...
]

并迭代它