Gar*_*yle 3 angularjs ng-repeat angularjs-ng-repeat
我在AngularJS中有一个简单的重复表行
<tr ng-repeat="item in items">
<td>{{ item.prop1 }}</td>
<td>{{ item.prop2 }}</td>
</tr>
Run Code Online (Sandbox Code Playgroud)
我的item对象上有一个comment属性,如果该注释包含一个值,我想在其余元素之后的一行上直接在一行中显示该值,并使其覆盖整个行。这种事情有可能吗?我看着ng-repeat-end,但这似乎不是我想要的,而且我不能只在ng-repeat内添加和添加<tr>元素,因为那将是无效的HTML。
您可以通过和使用ng-repeat的特殊重复起点和终点。ng-repeat-startng-repeat-end
的HTML
<table ng-controller="PostController">
<tr ng-repeat-start="post in posts">
<td>{{post.content}}</td>
</tr>
<tr ng-repeat-end ng-repeat="comment in post.comments">
<td>{{comment.content}}</td>
</tr>
</table>
Run Code Online (Sandbox Code Playgroud)
更新:如果注释是字符串,则只需ng-repeat在ng-repeat-end结尾处删除,然后添加一个ng-if表达式来验证的存在post.comment。
的HTML
<table ng-controller="PostController">
<tr ng-repeat-start="post in posts">
<td>{{post.content}}</td>
</tr>
<tr ng-repeat-end ng-if="post.comment">
<td>{{post.comment}}</td>
</tr>
</table>
Run Code Online (Sandbox Code Playgroud)