选择具有重新成角度或成角度的嵌套阵列元素

Select nested array element with restangular or angular

本文关键字:嵌套 元素 阵列 新成角 选择      更新时间:2023-09-26

我似乎很难使用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>

如果您想通过$scope.data 进行操作,您可以做些什么

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

其中项目对应于阵列中的每个元素

如果要通过$scope.data.ip地址进行操作:

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

您可以使用:

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

使用ng重复:

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

感谢大家的帮助。

最终,这对我来说是有效的:是的,我知道我用了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>