选择具有restangular或angular的嵌套数组元素

时间:2015-03-27 08:48:09

标签: javascript angularjs mongodb mongoose restangular

我似乎无法使用restangular或angular从mongo / mongoose返回的数组中读取/查询嵌套的json元素。

我可以写入嵌套元素,但不能读取/查询。

为了澄清数据,请返回并保存在$ scope.data中。但是我如何写restangular或angular来获得二级键和&来自$ scope.data的值。

更新 - 额外信息

这是$ scope.data中的数据:

[
{
    "_id": "551528ecbb8253446cb26e4f",
    "location": "London Office",
    "hostname": "lon-asa-01",
    "device": "Switch-MLS",
    "model": "Cisco 3750",
    "softwareImage": "1.2",
    "serialNumber": "123456",
    "subnets": [
        {
            "range": "10.0.100.0/24",
            "type": "Client"
        },
        {
            "range": "10.0.101.0/24",
            "type": "Server"
        }
    ],
    "ipAddresses": [
        {
            "ipAddress": "10.0.100.1",
            "type": "Inside"
        },
        {
            "ipAddress": "10.0.101.254",
            "type": "Outside"
        }
    ]
}
]

使用angular我希望能够{{data.ipAddresses.ipAddress}}和{{data.ipAddresses.type}}来获取嵌套元素中的键/值对,这样我就可以

<tr>
    <td>{{ data.ipAddresses.type }}</td>
    <td>{{ data.ipAddresses.ipAddress }}</td>
</tr>

3 个答案:

答案 0 :(得分:1)

如果你想通过$ scope.data

进行操作,你可以做些什么
<table>
  <tr ng-repeat="item in data">
    <td>
     {{item.ipAddresses[0].ipAddresses}}
     {{item.ipAddresses[0].type}}

    </td>
  </tr>
</table>

其中item对应于数组中的每个元素

如果你想通过$ scope.data.ipAddresses进行操作:

<table>
  <tr ng-repeat="item in data[0].ipAddresses">
    <td>
     {{item.ipAddresses}}
     {{item.type}}

    </td>
  </tr>
</table>

答案 1 :(得分:0)

您可以使用:

<tr>
    <td>{{ data[0].ipAddresses[0].type }}</td>
    <td>{{ data[0].ipAddresses[0].ipAddress }}</td>
</tr>

使用ng-repeat:

<tr ng-repeat="item in data[0].ipAddresses">
    <td>{{ item.type }}</td>
    <td>{{ item.ipAddress }}</td>
</tr>

答案 2 :(得分:0)

谢谢大家的帮助。

最后这对我有用:是的,我知道我使用的是ul li,但我只是原型设计。

<ul ng-repeat="network in networks">
            <li>{{ network._id}}</li>
            <li>{{ network.location}}</li>
            <li>{{ network.hostname}}</li>
            <li>{{ network.device}}</li>
            <li>
                <ul ng-repeat="ip in network.ipAddresses">
                    <li>{{ ip.type }}</li>
                    <li>{{ ip.ipAddress }}</li>
                </ul>
            </li>

            <li>{{ network.subnets}}</li>
            <li>{{ network.iosVersion}}</li>
            <li>{{ network.softwareImage}}</li>
            <li>{{ network.serialNumber}}</li>

        </ul>