简体   繁体   English

Angular2 Reactive Forms - 从条件动态禁用表单控制

[英]Angular2 Reactive Forms - Disable form control dynamically from condition

I have a select control that I want to disable dynamically based on a condition:我有一个选择控件,我想根据条件动态禁用它:

this.activityForm = this.formBuilder.group({
  docType: [{ value: '2', disabled: this.activeCategory != 'document' }, Validators.required]
});

However, docType doesn't become enabled even though at some point this.activeCategory becomes 'document'.但是,即使在某些时候 this.activeCategory 变为“文档”,docType 也不会启用。

How do I fix this?我该如何解决?

Since I don't know how you're manipulating activeCategory (maybe it's also a FormControl ?), I'll suggest the following approach:因为我不知道你是如何操作activeCategory (也许它也是一个FormControl ?),我会建议以下方法:

You can use (change) to detect when this.activeCategory has changed, as below:您可以使用(change)来检测this.activeCategory发生变化,如下所示:

1 - If you're using ngModel : 1 - 如果您使用的是ngModel

<input type="text" [(ngModel)]="activeCategory" (change)="checkValue($event)">

2 - If it's a FormControl : 2 - 如果它是一个FormControl

<input type="text" formControlName="activeCategory" (change)="checkValue($event)">

So, in component, you can manipulate the docType control using disable/enable methods:因此,在组件中,您可以使用disable/enable方法操作docType控件

checkValue(event: Event) {
  const ctrl = this.activityForm.get('docType');

  if (event.value === 'document') {
    ctrl.enable();
  } else {
    ctrl.disable();
  }
}

You have to handle select elements differently than you do other HTML elements.您必须以不同于处理其他 HTML 元素的方式处理 select 元素。 You will have to perform a reset when this.activeCategory changes.this.activeCategory更改时,您必须执行重置。

Something like this:像这样的东西:

this.activityForm.controls['docType'].reset({ value: '2', disabled: false });

Of course, you will probably want to use the current value, rather than a hard coded '2' .当然,您可能希望使用当前值,而不是硬编码的'2'

Same sort of thing to disable it, if that need arises (and it probably will).如果需要(并且可能会),可以禁用它。

this.activityForm.controls['docType'].reset({ value: '2', disabled: true });

More information on ng form control reset .有关 ng form control reset 的更多信息。

from .ts file you can add keys to yours from controls, and there you add the item disabled:true (desactivate form item) or false (activate form item)从 .ts 文件中,您可以从控件向您的密钥添加密钥,然后添加项目 disabled:true(停用表单项目)或 false(激活表单项目)

.ts .ts

public form_name=new FormGroup({

              series: new FormControl({value: '', disabled: true}),
              inten: new FormControl({value: '', disabled: true}),
              rep: new FormControl({value: '', disabled: true}),
              rest: new FormControl({value: '', disabled: false}),
              observation: new FormControl({value: '', disabled: false}),



  });

.html .html

<form  [formGroup]="form_name" >

                                          <label for="series"  >Series</label>
                                           <input type="text" formControlName="series" >

                                          <label for="inten"  >Inten</label>
                                           <input type="text" formControlName="inten" >

                                          <label for="rep"  >Rep</label>
                                           <input type="text" formControlName="rep" >

                                           <label for="rest"  >rest</label>
                                            <textarea formControlName="rest" rows="3" cols="30" ></textarea>


                    </form>

this.activityForm.controls['docType'].disable();

you can use ternary operator here to disable form control conditionally.您可以在此处使用三元运算符有条件地禁用表单控件。

this.activityForm = this.formBuilder.group({
docType: [{ value: '2', disabled: this.activeCategory != 'document' ? true : false }, Validators.required]
});


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

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