如何在angular2中的数组中的数组中设置补丁值(更新嵌套值)?

时间:2017-05-03 04:54:35

标签: angular angular2-routing angular2-forms angular2-directives

我有一个像

这样的嵌套数组
permission": [
{
  "id": 2,
  "key": "creatBusinessPermission",
  "legend": "BusinessModule",
  "ischecked": false,
  "label": " Create Business Settings",
  "roles": [
    {
      "id": 1,
      "key": "self",
      "ischecked": false
    },
    {
      "id": 2,
      "key": "selfrole",
      "ischecked": false
    },
    {
      "id": 3,
      "key": "other",
      "ischecked": false
    },
    {
      "id": 4,
      "key": "All",
      "ischecked": false
    }
  ]
},
{
  "id": 3,
  "key": "editBusinessPermission",
  "legend": "BusinessModule",
  "ischecked": true,
  "label": " Edit Business Settings",
  "roles": [
    {
      "id": 1,
      "key": "self",
      "ischecked": false
    },
    {
      "id": 2,
      "key": "selfrole",
      "ischecked": false
    },
    {
      "id": 3,
      "key": "other",
      "ischecked": false
    },
    {
      "id": 4,
      "key": "All",
      "ischecked": false
    }
  ]
}]

对于上面的数组我正在使用反应形式, 构建一个表单后,我有一个函数用于onchange的权限,当调用该函数的权限相关缺血和自我缺血改变为真时,我完成了pesrmission被检查为真正的完美, 我正在努力使**自我**缺血为真,

这是我的代码

 (<FormArray>this.roleForm.controls['permission']).at(index).patchValue({
      ischecked : true
    }) This is works fine

我在这里无法做什么

(<FormArray>this.roleForm.controls['permission']).at(index).get('roles').at(1)..patchValue({
      ischecked : true
    }) this code giving error, Probebly I did n't no how to do

1 个答案:

答案 0 :(得分:2)

试试这个

let x = (<FormArray>this.roleForm.controls['permission']).at(index).get('roles')
let y =   (<FormControl>x.controls[0])

y.patchValue({
        ischecked: true
      })