如何清除 Angular 反应式表单输入值

Men*_*ena 5 angular angular-reactive-forms

我有一个像这样设置的简单反应形式

constructor(private scheduleService: ScheduleService, fb: FormBuilder) {
this.searchForm = fb.group({
  from: ["", Validators.required],
  to: ["", Validators.required],
  date: ["", Validators.required]
});
}
Run Code Online (Sandbox Code Playgroud)

我想通过单击调用清除函数的按钮来清除表单字段。我的清除功能目前不起作用,看起来像这样

clear() {
console.log("Reset all search form fields to null.");

this.searchForm.from = "";
this.searchForm.to = "";
this.searchForm.date = "";
}
Run Code Online (Sandbox Code Playgroud)

使用角度反应表单时清除表单输入字段的正确方法是什么?如何通过使用单个语句重置所有表单字段而不是单个字段来实现此目的?

San*_*eva 12

  ngOnInit(): void {
    this.searchForm = this.fb.group({
      from: ['', Validators.required],
      to: ['', Validators.required],
      date: ['', Validators.required]
    });
  }
Run Code Online (Sandbox Code Playgroud)

重置完整表格

  resetForm() {
    this.searchForm.reset();
  }
Run Code Online (Sandbox Code Playgroud)

重置个别字段

  resetFrom() {
    this.searchForm.get('from').reset();
  }
Run Code Online (Sandbox Code Playgroud)