在 Angular 7 应用程序中,使用反应式表单,我如何修改此方法,以便它只清除表单组中的无效表单控件?
In an Angular 7 application, with reactive forms, how can i modify this method so that it will clear only invalid form controls within a form group?
我需要修改此方法,以便当用户单击按钮时,它们所在的表单组中的所有无效表单控件都将其值重置或设置为 null。
当前的方法包括:
disableControl(group: FormGroup){
Object.keys(group.controls).forEach((key: string) => {
const abstractControl = group.get(key);
abstractControl.setValue(null)
abstractControl.disable();
})
}
当用户单击按钮时,任何无效的表单控件都应设置为空或重置。
您可以检查表单控件是否为invalid
:
disableControl(group: FormGroup) {
Object.keys(group.controls).forEach((key: string) => {
const abstractControl: AbstractControl = group.get(key);
if (abstractControl.invalid) {
abstractControl.reset();
// .... or
// abstractControl.setValue(null);
}
})
}
我需要修改此方法,以便当用户单击按钮时,它们所在的表单组中的所有无效表单控件都将其值重置或设置为 null。
当前的方法包括:
disableControl(group: FormGroup){
Object.keys(group.controls).forEach((key: string) => {
const abstractControl = group.get(key);
abstractControl.setValue(null)
abstractControl.disable();
})
}
当用户单击按钮时,任何无效的表单控件都应设置为空或重置。
您可以检查表单控件是否为invalid
:
disableControl(group: FormGroup) {
Object.keys(group.controls).forEach((key: string) => {
const abstractControl: AbstractControl = group.get(key);
if (abstractControl.invalid) {
abstractControl.reset();
// .... or
// abstractControl.setValue(null);
}
})
}