ng-repeat is not working as expected

二次信任 提交于 2019-12-25 14:07:40

问题


Below object should generate table rows for parent and child objects under one table(as siblings). how it can be, I used ng-repeat-start but no use. I need it for equal alignments.

[
{'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 should generate like below:

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

回答1:


You may try

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



回答2:


All is ok, because this code

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

is out of this scope

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

if you want to achieve what you want you can make a flat array like this

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

and iterate over it



来源:https://stackoverflow.com/questions/28578025/ng-repeat-is-not-working-as-expected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!