在组件类中使用ViewChild装饰器来引用表单,并在需要重置表单时,调用表单的reset方法。
示例代码:
import { Component, ViewChild } from ‘@angular/core’;
@Component({ selector: ‘app-my-form’, templateUrl: ‘./my-form.component.html’, styleUrls: [‘./my-form.component.css’] }) export class MyFormComponent { @ViewChild(‘myForm’) myForm;
firstName: string; lastName: string;
onSubmit() { console.log(this.firstName, this.lastName); }
resetForm() { this.myForm.reset(); } }
这样,当用户点击“重置”按钮时,表单将被重置并清空所有输入字段。