Angular 6 FormGroup.disable()方法不适用于我的模板驱动的NgForm [英] Angular 6 FormGroup.disable() method is not working with my template driven NgForm

查看:608
本文介绍了Angular 6 FormGroup.disable()方法不适用于我的模板驱动的NgForm的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试在Angular 6应用程序的formGroup上使用disable方法时,在浏览器控制台中出现此错误:

When I try to use the disable method on a formGroup in my Angular 6 app , I get this error in the browser console :

TypeError:this.personForm.disable不是函数

TypeError: this.personForm.disable is not a function

尽管该方法是文档中提到的

Although the method is

我的代码在这里:

// ... 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:

我创建了一个堆栈闪电来显示问题

I have created a stackblitz to show the issue

此处是该演示的链接

更新2: 由于错误不会在初始加载时显示,因此如果您删除this.firstStepForm.disable();&重写它,您将收到错误,但无论如何行为不正确,未按预期禁用表单字段

UPDATE 2: As the error is not shown on initial loading, if you remove the this.firstStepForm.disable(); & rewrite it , you will get the error, but anyway the behavior is not correct, the form field is not disabled as expected

此外,刷新stackblitz中的浏览器部分将显示错误的小吃栏

Also, refreshing the browser part in the stackblitz will show an error snackbar

推荐答案

我发现了此错误的原因:

I have discovered the reason for this error:

此表单是使用 ngForm 在html模板中创建的,然后我使用ViewChild来保存打字稿文件中的表单,我注意到我做了 FormGroup类型的对象,但是ngForm与FormGroup不同(在我的用例中尚不清楚),这就是为什么 FormGroup disable方法不适用于ngForm

This form is made in the html template using ngForm, then I used ViewChild to get hold of the form in the typescript file, I noticed that I made that object of type FormGroup but ngForm is different than FormGroup (in my use cases it wasn't clear) that is why the FormGroup disable method is not working on the ngForm

注意:

(VS代码建议使用该变量,因为我对该变量的类型是FormGroup,这会误导编辑人员给我该建议)

(VS code is suggesting it as my type for that variable is FormGroup which is misleading the editor to give me that suggestion)

感谢所有尝试提供帮助的人.

Thanks for everyone who tried to help.

更新:

万一有人不愿像我一样依赖detectChanges() &基于出色的Amit答案,我们可以在AfterContentChecked中禁用NgForm,以避免使用detectChanges()

In case someone is reluctant to depend on detectChanges() like me & Building upon the excellent Answer of Amit , we can disable the NgForm in this AfterContentChecked to avoid using detectChanges()

  // instead of disabling here & using cdr.detectChanges()

  // ngAfterViewChecked() {
  //     console.log(this.personForm.form);    
  //     this.personForm.form.disable(); 
  //     this.cdr.detectChanges();  
  // } 

  // we can put our disable here
  ngAfterContentChecked() {
    this.personForm.form.disable();
  }

Stackblitz演示

这篇关于Angular 6 FormGroup.disable()方法不适用于我的模板驱动的NgForm的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆