【发布时间】:2018-12-12 01:05:15
【问题描述】:
当我尝试在 Angular 6 应用程序中的 formGroup 上使用 disable 方法时,我在浏览器控制台中收到此错误:
TypeError: this.personForm.disable 不是函数
虽然方法是mentioned in the documentation & 甚至 VS Code 都建议使用此快照。
我的代码在这里:
// ... many parts skipped, the form here is template driven
// but still personForm is a FormGroup ,
// I am trying to disable the whole FormGroup with all child elements
@ViewChild('personForm') personForm: FormGroup;
if (true) {
// if I console.log here the form, I can see it is created already
console.log(this.personForm);
// output of console.log is
// NgForm {submitted: false, _directives: Array(0), ngSubmit: EventEmitter, form: FormGroup}
this.personForm.disable();
}
这里有什么问题?
更新 1:
我创建了一个堆栈闪电战来显示问题
here is the link for that demo
更新 2:
由于初始加载时错误未显示,如果您删除 this.firstStepForm.disable(); 并重写它,您将收到错误,但无论如何行为不正确,表单字段未按预期禁用
另外,在 stackblitz 中刷新浏览器部分会显示错误的snackbar
【问题讨论】:
-
你的 if 条件到底是在哪里检查的?在 NgOnInit 中?
-
@monogate 是的,在那里检查过
-
您可以尝试实现 AfterViewInit 并将您的代码放入 ngAfterViewInit 中吗?
-
我会试试这个,我还检查了 FormGroup 是由 console.log 创建的,它是在调用之前创建的(请检查问题中的代码,我已经更新了)跨度>
-
不幸的是,如果我将 disable() 放在 ngAfterViewInit 中,我仍然会遇到同样的错误
标签: angular typescript angular6 angular-forms