简体   繁体   English

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

[英]Accessing the value of a control in a form array in angular2

Am setting my form via form builder with an array of a rest api result我正在通过表单构建器设置我的表单,其中包含一个其余的 api 结果数组

This is my form builder code这是我的表单构建器代码

 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)
      })

    },
  );

Now in my form现在以我的形式

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

And the error returned is返回的错误是

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

Where am i going wrong我哪里错了

There is no form control with a name of "checklists.controls["noradio"]", it would bind with [formControl]="checklists.controls['noradio']" however because then it attaches directly to the control object.没有名称为 "checklists.controls["noradio"]" 的表单控件,它将与[formControl]="checklists.controls['noradio']"绑定,但是因为它直接附加到控件对象。 [formControlName]="'noradio'" might also work but FormArray access and control is not my strong suit. [formControlName]="'noradio'"也可能有效,但 FormArray 访问和控制不是我的强项。

The syntax between the two is different enough that it warrants further reading .两者之间的语法完全不同,值得进一步阅读

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM