ng-repeat对象数组不起作用..?

时间:2014-07-22 18:48:00

标签: angularjs angularjs-ng-repeat

我有一个像这样的对象数组:

var data = [{
    "id": 1,
    "age": 20,
    "name": "Janell Mcintosh",
    "gender": "female",
    "email": "janellmcintosh@zilphur.com",
    "phone": "+1 (906) 555-2575"
  },etc..
];
var keys = Object.keys(data[0]);

我试图在表格中显示这些数据。键是列,所有对象的值都应该放在相关列中。这是我正在尝试的:

<table class="table table-striped">
    <tr>
        <th ng-repeat="key in keys">
            {{key}}
        </th>
    </tr>

    <tr ng-repeat="row in data">
        <td ng-repeat="key in keys">
            {{row['"'+key+'"']}}
        </td>
    </tr>
</table>

这不起作用。我试图在密钥周围加上引号,因为我收到的JSON在引号中有键。数据/变量存储正确,我只是不知道如何访问它们。

我在这里做错了吗?

{{row['"'+key+'"']}}

我只是想在键上添加引号。

View sample plunker here:

1 个答案:

答案 0 :(得分:2)

您不需要键值周围的双引号。 (无论引号是否存在,Javascript都会解析相同的键,因此var data = { "id": 1 }完全等同于var data = { id: 1 }

所以你需要的只是:

<td ng-repeat="key in keys">
    {{row[key]}}
</td>

here

相关问题