简体   繁体   English

如何在使用FormBuilder创建时禁用完整的表单组?

[英]How to disable the complete formgroup at creation with FormBuilder?

I'm building a Fromgroup with the FormBuilder. 我正在使用FormBuilder构建一个Fromgroup。

Example: 例:

  formName = this.fb.group({
    inputName: ['default value', Validators.required],
    // many other items
  });

(Example from: https://stackblitz.com/edit/form-reactive-formbuilder ) (例如: https//stackblitz.com/edit/form-reactive-formbuilder

Now based on a condition, I want this form to be disabled when loading the vie. 现在根据条件,我希望在加载视图时禁用此表单。 Currently I'm using 目前我正在使用

if([Condition]) {
   formName.disable();
}

However this are three extra lines of code an I want to pass the disable argument when creating the form group, like it can be done with FormControls. 然而,这是三行额外的代码,我想在创建表单组时传递disable参数,就像可以使用FormControls一样。 Can this be done? 可以这样做吗?

Forms created via FormsBuilder require you to use the disable() method in order to have it disabled. 通过FormsBuilder创建的FormsBuilder要求您使用disable()方法以禁用它。

Personally, in these scenariouses, I wrap the form with a container div and use an additional property that marks that the form is being loaded (or that we haven't finished yet). 就个人而言,在这些场景中,我用一个容器div包装表单,并使用一个额外的属性来标记表单正在加载(或者我们还没有完成)。 Then, I use the property in an *ngIf to physically remove the whole form and display a spinner o something similar. 然后,我使用*ngIf的属性来物理删除整个表单并显示一个类似的微调器。

Alternatively, I use the property to control the CSS that disables the container div in a more clever way. 或者,我使用该属性来控制以更聪明的方式禁用容器div的CSS。 The CSS depends on your context. CSS取决于您的上下文。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM