构建表单时控件和 AbstractControls 出错
Error with controls and AbstractControls when building a 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(),
})
]),
});
当我提交时,我有这个表格:
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);
}
}
}
工作正常,但是当我尝试 ng build --prod
时出现错误:
Property 'controls' does not exist on type 'AbstractControl'
。如何访问 FormArray 中 FormGroup 的控件?
提前致谢。
问题是您必须像这样将组专门转换为 FormGroup:
for (const group of (this.myForm.get("points") as FormArray).controls as FormGroup[]) {
我有这个表格:
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(),
})
]),
});
当我提交时,我有这个表格:
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);
}
}
}
工作正常,但是当我尝试 ng build --prod
时出现错误:
Property 'controls' does not exist on type 'AbstractControl'
。如何访问 FormArray 中 FormGroup 的控件?
提前致谢。
问题是您必须像这样将组专门转换为 FormGroup:
for (const group of (this.myForm.get("points") as FormArray).controls as FormGroup[]) {