0

我有一个看起来像这样的对象:

在此处输入图像描述

所以基本上我不知道对象的长度,我使用以下代码来呈现结果:

<table style="width: 100%" border="0" cellpadding="0" cellspacing="0">
    <tr ng-repeat-start="(key, item) in items() track by key" ng-if="item.primary"></tr>
        <td first someprop="2">first</td> 
    <tr ng-repeat-end ng-if="!item.primary">
        <td>[NOT] first</tr>
    </tr>
</table>

我希望表中的第一个元素(它也突出显示)始终是具有该primary=true属性的对象。

上面表示的对象的预期结果:

在此处输入图像描述

当前结果:

在此处输入图像描述

如果我向对象添加第三个元素,我会得到:

在此处输入图像描述

小提琴:http : //jsfiddle.net/UuzZT/

4

2 回答 2

1

You have a simple semantic error in your HTML, specifically the ng-repeat-start element does not wrap the <td> so the <td> shows up each time even though the ng-if is there. Corrected here:

http://jsfiddle.net/UuzZT/1/

However, there are other problems stemming from the fact that you are trying to iterate over an object; objects do not have ordinal properties.

于 2014-02-07T03:37:24.157 回答
0

您的第一个表格行不包含表格数据(td 外部 tr)

将您的代码更改为:

<table style="width: 100%" border="0" cellpadding="0" cellspacing="0">
<tr ng-repeat-start="(key, item) in items() track by key" ng-if="item.primary">
    <td first someprop="2">first</td>
</tr> 
<tr ng-repeat-end ng-if="!item.primary">
    <td>[NOT] first</tr>
</tr>

于 2014-02-07T09:12:27.640 回答