Issue
I have this form :
this.myForm = new FormGroup({
points: new FormArray([
new FormGroup({
date: this.date,
startTime: new FormControl(null, Validators.required),
endTime: new FormControl(null, Validators.required),
}),
new FormGroup({
date: this.date,
startTime: new FormControl(),
endTime: new FormControl(),
}),
new FormGroup({
date: this.date,
startTime: new FormControl(),
endTime: new FormControl(),
})
]),
});
When I submit I have this form :
for (const group of (this.myForm.get('points') as FormArray).controls) {
console.log(group);
if (group.controls.date !== null) {
if (group.controls.date.value !== null) {
group.controls.startTime.setValue(
group.controls.date.value + ' ' + group.controls.startTime.value
);
group.controls.endTime.setValue(
group.controls.date.value + ' ' + group.controls.endTime.value
);
group.controls.date.setValue(group.controls.date.value);
}
}
}
Works just fine but when I try to ng build --prod
I have the error :
Property 'controls' does not exist on type 'AbstractControl'
. How can I access the controls of a FormGroup in a FormArray?
Solution
The problem is that you have to cast the group specifically to a FormGroup as such:
for (const group of (this.myForm.get("points") as FormArray).controls as FormGroup[]) {
Answered By - Gecko
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.