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

时间:2022-12-02 15:45:17

I have an array of objects like this:

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

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]);

I am trying to display this data in a table. The keys are columns, and all of the object's values are supposed to go in the relevant column. Here is what I am trying:

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

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

This doesn't work. I am trying to put quotes around the key because the JSON I receive has the keys and values in quotes. The data/variables are stored properly, I just don't know how to access them.

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

Am I doing something incorrectly here?

我在这里做错了吗?

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

I'm just trying to add quotes to the key.

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

View sample plunker here:

在这里查看样品plunker:

1 个解决方案

#1


2  

You don't need the double-quotes around the key values. (Javascript parses keys the same whether or not the quotes are present, so var data = { "id": 1 } is completely equivalent to var data = { id: 1 }.

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

So all you need is:

所以你需要的是:

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

See here.

#1


2  

You don't need the double-quotes around the key values. (Javascript parses keys the same whether or not the quotes are present, so var data = { "id": 1 } is completely equivalent to var data = { id: 1 }.

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

So all you need is:

所以你需要的是:

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

See here.