【问题标题】:Select nested array element with restangular or angular选择带有restangular或angular的嵌套数组元素
【发布时间】:2015-05-31 12:50:24
【问题描述】:

我似乎很难从使用 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.ipAddresses = $scope.data.ipAddresses,这会给你一个对象数组:"ipAddresses": [ {"ipAddress": "10.0.100.1", " type": "Inside" }, { "ipAddress": "10.0.101.254", "type": "Outside" } ] 所以你应该能够使用 ng-repeat 遍历 $scope.ipAdresses。
  • 嗨 Jax,我已经完成了以下操作,但 $scope.ipAddresses 在 ng-repeat 或 {{ ipAddresses.type }}、{{ ipAddresses.ipAddress }} 中似乎不可用?

标签: javascript angularjs mongodb mongoose restangular


【解决方案1】:

谢谢大家的帮助。

最后这对我有用:是的,我知道我使用了 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>

【讨论】:

    【解决方案2】:

    如果你想通过 $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>
    

    【讨论】:

      【解决方案3】:

      你可以使用:

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

      【讨论】:

      • 这在一定程度上有效,但如果我有多个顶级记录设置 [0] 为所有人放置相同的 .type 和 .ipAddress?我怎样才能解决这个问题?
      猜你喜欢
      • 2011-04-04
      • 1970-01-01
      • 1970-01-01
      • 2011-12-04
      • 2019-07-24
      • 1970-01-01
      • 1970-01-01
      • 2015-12-08
      • 1970-01-01
      相关资源
      最近更新 更多