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

标签 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
    }
  ]
}]

对于上面的数组,我使用响应式(Reactive)形式, 构建表单后,我有一个关于 onchange 权限的函数,当调用该函数时,会检查与权限相关的内容,并且 self 会检查更改为 true,我完成的权限检查为 true 完美, 我正在努力使** self **被检查为真实,

这是我的代码

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

最佳答案

试试这个

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

y.patchValue({
        ischecked: true
      })

关于angular - 如何在 angular2 的数组中的数组中设置补丁值(更新嵌套值)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43751364/

相关文章:

node.js - 将 Angular 安装到现有项目中

Angular 2 - 没有可怕 URL 的命名路由器导出

javascript - chrome/angular2 的 window.location.hash 解决方法

Angular2 react 形式错误显示

javascript - Angular2 和 Karma 仅运行一个测试套件,又称为“describe.only”

angular - 获取 ngComponentOutlet 的引用

javascript - 异步调用返回后重新加载页面内容

javascript - 无法读取 Angular 2 中的路由参数

javascript - angular 相当于 angularjs 状态

angular - formGroup 需要一个 FormGroup 实例