You need to move the form creation inside the subscribe method and wait for the response because it is an asynchronous call.
export class MaintenanceSurveyComponent implements OnInit {
myFormTemplate: any = [];
myFormGroup: FormGroup = new FormGroup({});
@Input() public user;
constructor(private maintenanceService: MaintenanceRequestService) {}
ngOnInit() {
this.myFormGroup = new FormGroup({});
this.maintenanceService.getSurveyquestions(this.user).subscribe((res: any) => {
this.myFormTemplate = JSON.parse(res);
let group = {};
this.myFormTemplate.forEach((input_template) => {
group[input_template.hMy] = new FormControl({ value: input_template.hMy });
});
this.myFormGroup = new FormGroup(group);
});
}
onSubmit() {
alert('SUCCESS!! :-)\n\n' + JSON.stringify(this.myFormGroup.getRawValue(), null, 4));
console.log(this.myFormGroup.getRawValue());
console.log('this is test');
}
}
CLICK HERE to find out more related problems solutions.