将JSon响应移动到下拉列表中

时间:2016-09-21 10:59:11

标签: angularjs json angularjs-ng-change

我有一个Json响应,如下所示

JSON:

   {
"json": {
    "response": {
      "servicetype": "1",
      "functiontype": "10011",
      "statuscode": "0",
      "statusmessage": "Success",
      "data": {
        "assignedroles": [
          {
            "roleid": 162,
            "rolename": "Admin",
            "roleinformation": {
              "A": [
                {
                  "1st floor": 10
                }
              ]
            }
          },
          {
            "roleid": 163,
            "rolename": "Resident",
            "roleinformation": {
              "A": [
                {
                  "1st floor": 10
                }
              ]
            }
          }
        ],
        "unassignedroles": [
          {
            "roleid": 36,
            "rolename": "Product Managers",
            "divisionlabel": "Department ",
            "subdivisionlabel": "Category",
            "roleinformation": {
              "QA": [
                {
                  "White Box Testing": 0
                }
              ]
            }
          },

          {
            "roleid": 108,
            "rolename": "Teacher",
            "divisionlabel": "Class",
            "subdivisionlabel": "Div",
            "roleinformation": ""
          },
          {
            "roleid": 115,
            "rolename": "Staff",
            "divisionlabel": "Class",
            "subdivisionlabel": "Section",
            "roleinformation": {
              "1": [
                {
                  "A": 0
                }
              ]
            }
          },
          {
            "roleid": 116,
            "rolename": "Student",
            "divisionlabel": "Class",
            "subdivisionlabel": "Section",
            "roleinformation": {
              "1": [
                {
                  "A": 0
                }
              ]
            }
          },
          {
            "roleid": 184,
            "rolename": "CANt_MANAGE_Gbase",
            "divisionlabel": "Division",
            "subdivisionlabel": "SubDivision",
            "roleinformation": {
              "Div": [
                {
                  "Sdiv": 0
                },
                {
                  "Sdiv1": 0
                }
              ]
            }
          },
          {
            "roleid": 191,
            "rolename": "Legend",
            "divisionlabel": "a",
            "subdivisionlabel": "b"
          }
        ]
      }
    }
  }
}

需要从json响应中获取已分配和未分配值的角色名称。我能够进入两个阵列,但无法同时移动两个阵列。

HTML:

<div class="form-group">
  <label class="control-label col-sm-10" for="groupz">Select Role*</label>
  <select class="form-control" name="role"
          ng-model="model.rolename"
          ng-change="GetAssignRole(model.rolename)">
    <option selected>Select Roles</option>
    <option ng-repeat="item in model.roles track by $index"
            value="{{item}}">{{item}}</option>
  </select>
  <div>

JS:

   UserService.getAssignRoles(json).then(function(response) {
     if (response.json.response.statuscode == 0 && response.json.response.statusmessage == 'Success')
{
     $scope.model.assignroles = [];
             var resdata =response.json.response.data;

    var unasresdata = response.json.response.data.unassignedroles;
    var assresdata = response.json.response.data.assignedtoles;

    $scope.model.assignroles.push(unasresdata);
    $scope.model.assignroles.push(assresdata);


              }

   });
        };

我无法弄清楚如何将两个数据移动到下拉列表中。如果在指定或未指定时某些内容为空,则会在响应中给出未定义的内容。但是我可以在assignRoles中获得两个数组。

1 个答案:

答案 0 :(得分:0)

Concat unassignedroles []和assignedroles []制作单个数组。只需尝试下面的代码。它可能对你有帮助。

UserService.getAssignRoles(json).then(function(response) {
     if (response.json.response.statuscode == 0 && response.json.response.statusmessage == 'Success')
    {
        $scope.model.assignroles = [],assignrolesArray = [];

        var unasresdata = response.json.response.data.unassignedroles;
        var assresdata = response.json.response.data.assignedtoles;

        assignrolesArray = unasresdata.concat(assresdata);

        $scope.model.assignroles = assignrolesArray;


    }

   });

HTML

<div class="form-group">
  <label class="control-label col-sm-10" for="groupz">Select Role*</label>
  <select class="form-control" name="role"
          ng-model="model.rolename"
          ng-change="GetAssignRole(model.rolename)">
    <option selected>Select Roles</option>
    <option ng-repeat="role in model.assignroles track by $index"
            value="{{role.rolename}}">{{role.rolename}}</option>
  </select>
  <div>