处理json响应数组

时间:2016-09-28 07:07:58

标签: angularjs json angularjs-ng-repeat

我有一个json响应,如下面给出的后端 JSON:

{
    "json": {
        "response": {
          "servicetype": "1",
          "functiontype": "10011",
          "statuscode": "0",
          "statusmessage": "Success",
          "data":{
                "roleid": 36,
                "rolename": "Product Managers",
                "divisionlabel": "Department ",
                "subdivisionlabel": "Category",
                "roleinformation": {
                  "QA": [
                    {
                      "White Box Testing": 10
                    }
                  ]
                }
              },
              {
                "roleid": 38,
                "rolename": "Managers",
                "divisionlabel": "Department ",
                "subdivisionlabel": "Category",
                "roleinformation": {
                  "QA": [
                    {
                      "Black Box Testing": 10
                    }
                  ]
                }
              }

          }
        }
      }
    }

我已将我的角色名称插入下拉列表$ scope.model.rolename。

如果我的角色名被选为产品经理,我想将我的$ scope.maxcount值赋予"White Box Testing": 10。它应该是动态的,根据选择值会改变。现在在下拉列表中,如果我选择管理器,$ scoope.maxcount for manager值将会改变。我已经完成了直到下拉,不知道在它之后处理。

JS:

` `UserService.getAssignRoles(json).then(function(response) {

 if (response.json.response.statuscode == 0 && response.json.response.statusmessage == 'Success')
{
    $scope.model.roles= [], assignrolesArray = [];

           assignrolesArray = unasresdata.concat(assresdata);

    $scope.model.assignroles = assignrolesArray;

}
});

HTML:

    <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>
  <input type = "text" ng-model=$scope.maxcount>

1 个答案:

答案 0 :(得分:0)

您可以将model.assignroles和范围值传递给此getassignRole()函数,然后您可以获取属性并将其设置为范围变量。

安装lodash库并注入它。

HTML

ng-change="getassignRole(model.assignroles, model)"

JS

$scope.getassignRole= function(model.assignroles, rolename){
  console.log(model.assignroles);
  var test = _.filter(model.assignroles, function(o){
    return o.rolename === rolename;
  })
  console.log(rolename);
  console.log(test[0].roleinformation);      
};

示例jsfiddle:https://jsfiddle.net/alpeshprajapati/5kpxzrgf/1/

然后您可以访问所需的属性。

希望它有所帮助。