从json获取数据但仅限于一条记录

时间:2016-02-18 15:02:11

标签: angularjs json

我是这个JSON:

{
  "success": true,
  "return": {
    "totalItem": 7,
    "totalPages": 1,
    "pageSize": 7,
    "items": {
      "phones": [
        "(48) 9999-9999"
      ],
      "users": {
        "manager": {
          "id_user": "5819",
          "name": "Síndico Ilhas Belas",
          "user": "sindico.teste@domain.com",
          "photo": "https://domain.amazonaws.com/files/upload/2015/07/25/5819.55b32d90e69ab3.05638898_873f970f8d259.jpg"
        },
        "employees": [
          {
            "id_user": "2",
            "name": "José Perez",
            "user": "pepe@domain.com",
            "photo": "https://.amazonaws.com/files/upload/2013/10/07/2.52523a0451c3c5.59697102_7a4188d3.jpg",
            "groups": [
              {
                "id_group": "33",
                "name": "Portaria"
              }
            ],
            "work_details": {
              "work_schedule": "8-12hs e 14-18hs",
              "work_activities": "Supervisionar os trabalhos de conservação"
            }
          },
          {
            "id_user": "15142",
            "name": "Marcos Rojas",
            "user": "rojas@c.com",
            "photo": "http://www.domain.com/themes/intra/img/sf_ele.gif",
            "groups": [
              {
                "id_group": "589",
                "name": "Zeladoria"
              }
            ],
            "work_details": {
              "work_schedule": "8h 12h",
              "work_activities": "Zeladoria"
            }
          },
          {
            "id_user": "18833",
            "name": "Portaria",
            "user": "teste@domain.com",
            "photo": "http://www.domain.com/themes/intra/img/sf_ele.gif",
            "groups": [
              {
                "id_group": "33",
                "name": "Portaria"
              }
            ],
            "work_details": {
              "work_schedule": "8hs por dia. 8-12hs e 14-18hs",
              "work_activities": "Supervisionar os trabalhos de conservação"
            }
          }
        ],
        "boardMembers": [
          {
            "id_user": "8189",
            "name": "Ana Maria",
            "user": "8189",
            "photo": "http://www.domain.com/themes/intra/img/sf_ela.gif",
            "groups": [
              {
                "id_group": "722",
                "name": "Subsíndico"
              }
            ]
          },
          {
            "id_user": "11442",
            "name": "Luciana Zath",
            "user": "lzath@mail.com",
            "photo": "http://www.domain.com/themes/intra/img/sf_ela.gif",
            "groups": [
              {
                "id_group": "1456",
                "name": "Conselho fiscal"
              }
            ]
          }
        ]
      }
    }
  }
}

由于manager只是一条记录,因此我无法在此代码中使用ng-repeat获取该记录

<div class="card" ng-repeat="(manager, name) in items">
    <pre>{{name}}</pre>
</div>

JSON返回所有数据,但我只需要返回manager数据,例如:

姓名,用户(电子邮件)和照片

和控制器:

// Controller of about.
appControllers.controller('aboutCtrl', function($scope, $mdBottomSheet, $mdToast, $mdDialog, About) {

    About.get(function(data) {
        $scope.items = data.return.items;
        console.log(data);
    })

}); // End of about controller.

employeesboardMembers可以正常使用ng-repeat,但是经理的单一记录,而不是

4 个答案:

答案 0 :(得分:0)

如果manager始终是单个对象,请执行以下操作:

 Manager: {{items.users.manager.name}}

然后重复雇员阵列

 <div ng-repeat="employee in items.users.employees">
    Employee:  {{employee.name}}

答案 1 :(得分:0)

appControllers.controller('aboutCtrl', function($scope, $mdBottomSheet, $mdToast,
    $mdDialog, About) {

    About.get(function(data) {
        $scope.items = data.return.items;
        $scope.manager = data.return.items.users.manager;       
        console.log(data);
    })
}); // End of about controller.

in html:

 <p>{{manager.name}}</p>
 <p>{{manager.user}}</p>

答案 2 :(得分:0)

如果items是数组,则只能使用ng-repeat遍历items列表。在您的JSON中,items是一个对象,因此您不会获得经理名称。只需使用{{items.users.manager.name}}获取经理姓名即可。另一方面,您可以使用ng-repeat来迭代items.users.employees,因为它的employees属性是一个数组。

<div class="card" ng-repeat="employee in items.users.employees">
    <pre>{{employee.name}}</pre>
</div>

答案 3 :(得分:-2)

您可以使用

访问它
{{items.users.manager}}