angular - 如何以 angular2 react 形式迭代形式数组(数组中的数组中的数组)?

标签 angular angular2-template angular2-forms angular2-formbuilder

我多次尝试迭代 formArray,

这是这个案例的 plunker 链接 https://plnkr.co/edit/4kiJF7cL5VKwn3KnvjvK?p=preview

我想要像这样的笨蛋 https://plnkr.co/edit/zg6nbFULl0WlTZ1sVn5h?p=preview

这是我的场景

[

{
  "id": 1,
  "legend": "businessModule",
  "group": [

    {
      "id": 1,
      "permission": {
        "controleType": "ff",
        "id": 2,
        "key": "create Business"
      },
      "roles": [
        {
          "id": 1,
          "name": "self"
        },
        {
          "id": 2,
          "name": "other"
        }
      ]
    },


    {
      "id": 1,
      "permission": {
        "controleType": "ff",
        "id": 2,
        "key": "edit business"
      },
      "roles": [
        {
          "id": 1,
          "name": "self"
        },
        {
          "id": 2,
          "name": "other"
        }
      ]
    }

  ]
},


{
  "id": 2,
  "legend": "PanicModule",
  "group": [

    {
      "id": 1,
      "permission": {
        "controleType": "ff",
        "id": 2,
        "key": "create panic"
      },
      "roles": [
        {
          "id": 1,
          "name": "self"
        },
        {
          "id": 2,
          "name": "other"
        }
      ]
    },


    {
      "id": 1,
      "permission": {
        "controleType": "ff",
        "id": 2,
        "key": "edit panic"
      },
      "roles": [
        {
          "id": 1,
          "name": "self"
        },
        {
          "id": 2,
          "name": "other"
        }
      ]
    }

  ]
}

];

对于上面的数组,我尝试构建响应式表单,我需要下的permission, roles array复选框

所以我试着像这样迭代数组

component.ts

validateForm() {
  this.roleForm = this.fb.group({
  roleName: ['', Validators.required],
  roleDescription: ['', Validators.required],
  permissionModules: this.fb.array([])
  });
   this.initModule()
 }

初始化模块(){

  const contractArray = <FormArray>this.roleForm.controls['permissionModules'];
  console.log(contractArray, 'contractArray')
   this.form_objects.forEach(element => {
       this.newElement = element;
       console.log(this.newElement, 'this.newElement')
    // element.forEach(group => {

          contractArray.push(this.fb.group({
            id: [''],
            legend:this.newElement.legend,
            group: this.fb.array([ ])
       }));
    // }
  this.initGroup(element, contractArray)
   }
}



 initGroup (element, contractArray) {
    console.log(element, '@@@@@@@@@@@@@@@@@@@@@@',contractArray)
   // const groupArray = <FormArray>contractArray.controls['group'];
    this.groupArray = <FormArray>this.roleForm.controls.permissionModules.controls[1];

    console.log(this.groupArray, 'groupArray&&&&&&&')
      element.group.forEach(group => {
       this.newGroup = group;
       console.log(this.newGroup, 'this.newGroup')
      /* if(typeof (this.groupArray) !== 'undefined') {
           this.groupArray.push(this.fb.group({
                id: [''],
                legend:this.newGroup.legend

        }));
       }*/

        }
      }

 submit(value) {
    this.result = value
}

我的 html 是这样的

 <form [formGroup]="roleForm"  (submit)="submit(roleForm.value)">
    <h3>Add trust</h3>

    <div class="form-group">
        <label>Name</label>
        <input type="text" class="form-control" formControlName="roleName">
    </div>

    <div class="form-group">
        <label>roleDescription</label>
        <input type="text" class="form-control" formControlName="roleDescription">
    </div>
     <div class="form-group">
        <label>permission roles</label>
        <div formArrayName="permissionModules">
          <div *ngFor="let contract of roleForm.controls.permissionModules.controls;
          let i=index" class="panel panel-default">
           <div  [formGroupName]="i">
               {{contract.value.legend}}
             <!-- <input type = "checkbox" formControlName="legend">
              {{contract.value.legend}}-->



           </div>


          </div>
        </div>

     </div>
   <button type="submit">Submit</button>

</form>

但我无法在上述情况下迭代第二级数组 在 initGroup () 函数 groupArray 下,我不知道我的错误是什么,我搜索了很多网站都只告诉了一级迭代,我是 angular2 的新手,所以请任何人帮助我 提前致谢

最佳答案

我的问题解决了

使用 <FormArray>this.roleForm.controls['permissionModules']).at(k).get('group') as FormArray

语法我可以迭代嵌套数组,

这是 plunk 链接,您可以在其中看到我的方法 http://plnkr.co/edit/AVqWKkUCNc1HjosOpUWf?p=preview

关于angular - 如何以 angular2 react 形式迭代形式数组(数组中的数组中的数组)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43291346/

相关文章:

angular - 将 Angular 4 与 GoJ 集成

angular - ngModel 不显示 mat-select 中对象的值

node.js - Angular2错误self.parentView.context ..未定义

Angular,如何从字符串解析模板并传递当前上下文变量

javascript - 未捕获( promise ): TypeError: Cannot read property 'component' of null

angular - 在 AOT 编译之前 Lint Angular 2、4、5 模板 html 文件的最佳方法是什么

angular - 过滤 Angular 2管道中的十进制值

javascript - Angular 2关注点击/事件后的第一个无效输入

angular - Angular Material 中的嵌套列表

Angular 2 嵌套可观察循环