Am creating my form following a result of an asynchronous call but am stuck at how i should pass the results of the asynchronous calll to the form builder
This is what ive tried
export class PerformInspectionPage implements OnInit {
checklists: any;
inspectionform: FormGroup;
ngOnInit(): any {
this.checklists = this.onGetItems(); //this is where i fetch the data not sure of
//another way to use.
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 the function that gets items which is still on the same component
onGetItems(){
return this._inspectionService.getChecklists()
.subscribe(
res=> {
this.checklists = res;
},
);
}
when i check console.log(this.inspectionform); it has an array of 0 items. How do i modify this to return result an asynchronous call
I have also tried
return this._inspectionService.getChecklists(this.truckParam)
.subscribe(
res=> {
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)
})
},
);
The problem with this is that the instance for formbuilder is never visible on the form
That is
On the form
<form [formGroup]="inspectionform" #newform > //this returns an error of
formGroup expects a FormGroup instance. Please pass one