在 Angular 中,动态表单字段的重置需要按照以下步骤进行。
@Component({
selector: 'app-my-form',
templateUrl: './my-form.component.html',
styleUrls: ['./my-form.component.css']
})
export class MyFormComponent implements OnInit {
form: FormGroup;
submitted = false;
constructor(private formBuilder: FormBuilder) { }
ngOnInit() {
this.form = this.formBuilder.group({
// define your form fields here
});
}
}
FormControl
控件并设置默认值。import { FormControl } from '@angular/forms';
this.form = this.formBuilder.group({
name: new FormControl(''),
email: new FormControl('')
});
false
,并使用默认值重置表单字段。resetForm() {
this.submitted = false;
this.form.reset();
}
true
。submitForm() {
this.submitted = true;
// handle form submission here
}
resetForm
方法上。
下一篇:Angular中转义字符问题