Angular 生成窗体时控件和抽象控件出错
我有以下表格:Angular 生成窗体时控件和抽象控件出错,angular,typescript,angular9,Angular,Typescript,Angular9,我有以下表格: this.myForm = new FormGroup({ points: new FormArray([ new FormGroup({ date: this.date, startTime: new FormControl(null, Validators.required), endTime: new FormControl(null, V
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);
}
}
}
当我提交时,我有以下表格:
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
时,出现错误:
类型“AbstractControl”上不存在属性“controls”
。如何访问FormArray中FormGroup的控件
提前感谢。问题是您必须将组专门转换为FormGroup:
for (const group of (this.myForm.get("points") as FormArray).controls as FormGroup[]) {
也许这可以通过专门将组强制转换为FormGroup来解决吗?@GeckoIT我试过了,但没有结果(const group of(this.myForm.get(“points”)作为FormArray)。控件作为FormGroup[]){…似乎在我这边起了作用。是的!谢谢