简体   繁体   中英

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

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. [formControlName]="'noradio'" might also work but FormArray access and control is not my strong suit.

The syntax between the two is different enough that it warrants further reading .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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