【发布时间】:2019-06-24 02:49:52
【问题描述】:
我正在尝试找到重置角度反应形式的最佳方法。我对重置反应式表单有点困惑,无法找到哪种方法适用于模板驱动的表单,哪种是反应式表单。 现在我使用“formGroupDirective”进行重置,但出现如下控制台错误。
这就是我使用 formGroupDirective 进行重置的方式。
模板文件:
<form
...
#formDirective="formGroupDirective"
>
TS 文件:
import { ViewChild, ... } from '@angular/core';
import { FormGroupDirective, ... } from '@angular/forms';
export class MyComponent {
...
@ViewChild('formGroupDirective') private formGroupDirective: FormGroupDirective;
constructor(... )
private someFunction(): void {
...
formGroupDirective.resetForm();
}
}
这里我不明白一件事,FormGroupDirective 和 FormDirective 有什么区别。哪一种更适合反应形式。 甚至我们可以通过像
这样的 formGroup 名称来重置this.formGroup.reset();
所以如果我们可以通过formGroup名称进行重置,那么为什么我们需要使用指令。如果有人有想法,请帮助我理解这些差异。
【问题讨论】:
标签: angular angular-reactive-forms