Angular从对象创建表

时间:2016-03-01 14:24:41

标签: angularjs

从Rest Api我得到以下内容:

{
 "headers": ["h1", "h2"],
 "body": [{"h1": "a1", "h2":"a2"},
          {"h1": "b1", "h2":"b2"},
           ...                   ]
}

现在我想将此变换为带有角度的(可订购表格)。我试过了:

<table class="table table-striped">
    <thead>
    <tr>
        <th ng-repeat="header in data.headers">{{ header }}</th>
    </tr>
    </thead>
    <tbody>
    <tr ng-repeat="line in data.body">
        <td ng-repeat="(key, val) in line">{{ val | date : "dd.MM.yy" }}</td>

    </tr>
    </tbody>
</table>

当然它不起作用,因为对象在javascript中没有键顺序。 是否有一种简单的方法可以按标题对行进行排序?

1 个答案:

答案 0 :(得分:3)

你可以这么做:

<tr ng-repeat="line in data.body">
    <td ng-repeat="header in data.headers">{{ line[header] | date : "dd.MM.yy" }}</td>
</tr>