在angular2中访问窗体数组中控件的值
我使用RESTAPI结果数组通过FormBuilder设置表单 这是我的表单生成器代码在angular2中访问窗体数组中控件的值,angular,typescript,Angular,Typescript,我使用RESTAPI结果数组通过FormBuilder设置表单 这是我的表单生成器代码 this._inspectionService.getChecklists(this.truckParam) .subscribe( res=> { this.checklists = res; let inspectionform: FormGroup; let checkinputs: FormArray = new FormArray([]);
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)
})
},
);
哪里出了问题没有名为“checklists.controls[“noradio”]”的表单控件,它将绑定到
[formControl]=“checklists.controls['noradio']”
,因为这样它就直接附加到控件对象<代码>[formControlName]=“'noradio'”也可以工作,但FormArray访问和控制不是我的强项
两者之间的语法差异很大,足以证明这一点。您的代码中的inputfileds是否也存在拼写错误,还是仅此而已?无论如何,试试formControlName='{{checklists.controls[“noradio”].value}}}'…它不是inspectionform.controls['''']…不,它在表单生成器上没有错,我实际上把它作为inputfileds,但是用控件访问FormBuilder对象,它被分配给
inspectionform
那么为什么不试试inspectionform.control['inputfileds]['noradio']
因为您要将它添加到该生成器中。#wesside正在遍历控件数组,从哪个单选按钮开始,我应该使用什么绑定,因为使用formControl绑定会导致两者都被选中。每个HTML单选按钮按名称分组,然后按其值区分开来。
:@GEOFFREYMWANGI以获取您的att因为我在第一次评论中忘记了。
<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"]'