我似乎很难使用 Restangle 或 Angular 从 mongo/mongoose 返回的数组中读取/查询嵌套的 json 元素。
我可以写入嵌套元素,但不能读取/查询。
为了澄清数据被返回并保存在 $scope.data 中。但是我如何编写 reangular 或 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.ipAddresses进行操作:
<table>
<tr ng-repeat="item in data[0].ipAddresses">
<td>
{{item.ipAddresses}}
{{item.type}}
</td>
</tr>
</table>
关于javascript - 选择具有矩形或 Angular 嵌套数组元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29296513/