加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

如何在angular2反应形式中迭代表单数组(数组中的数组)?

发布时间:2020-12-17 17:23:09 所属栏目:安全 来源:网络整理
导读:我试过几次迭代formArray, 这是这个案例https://plnkr.co/edit/4kiJF7cL5VKwn3KnvjvK?p=preview的plunker链接 我想要像这样插入https://plnkr.co/edit/zg6nbFULl0WlTZ1sVn5h?p=preview 这是我的情景 [{ "id": 1,"legend": "businessModule","group": [ { "id
我试过几次迭代formArray,

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

我想要像这样插入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,"key": "edit business"
      },"name": "other"
        }
      ]
    }

  ]
},{
  "id": 2,"legend": "PanicModule","key": "create panic"
      },"key": "edit panic"
      },"name": "other"
        }
      ]
    }

  ]
}

];

对于上面的数组我尝试构建反应式表单,我需要复选框的权限,组下的角色数组

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

component.ts

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

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’)作为FormArray

synatax我能迭代嵌套数组,

这是一个plunk链接,你可以看到我的方法
http://plnkr.co/edit/AVqWKkUCNc1HjosOpUWf?p=preview

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读