在angular2中访问表单数组中的控件的值

时间:2017-02-01 21:44:26

标签: angular typescript

我是通过表单构建器设置我的表单,其中包含一个rest api结果数组

这是我的表单构建器代码

 this._inspectionService.getChecklists(this.truckParam)
  .subscribe(
    res=> {
      this.checklists = res;
      let inspectionform: FormGroup;
      let checkinputs: FormArray = new FormArray([]);
      for (let i = 0; i < this.checklists.length; i++) {
        checkinputs.push(
          new FormGroup({
            description:new FormControl(this.checklists[i].item),
            input: new FormControl(''),
            yesradio: new FormControl(''),
            noradio: new FormControl(''),
          })
        )
      }

      this.inspectionform = this._formBuilder.group({
        inputfileds: this._formBuilder.array(checkinputs.controls)
      })

    },
  );

现在以我的形式

<form [formGroup]="inspectionform">

<ion-card *ngFor='let checklists of inspectionform.controls["inputfileds"]["controls"] 
 ;let i=index'>

//at this stage i can access
{{checklists.controls["description"].value}} //it retuns a value

//NOW AM trying to connect the form control inputs via

  <ion-input type="text" formControlName='checklists.controls["noradio"]'>
IVE ALSO TRIED
  <ion-input type="text" formControlName='checklists.controls.noradio'>

返回的错误是

Cannot find control with name: 'checklists.controls["noradio"]'

我哪里出错

1 个答案:

答案 0 :(得分:1)

没有名称为“checklists.controls [”noradio“]”的表单控件,它会与[formControl]="checklists.controls['noradio']"绑定,但因为它会直接附加到控件对象。 [formControlName]="'noradio'"也可能有效,但FormArray访问和控制不是我的强项。

两者之间的语法不同,它保证further reading