禁用反应形式的输入字段 [英] Disable Input fields in reactive form

查看:82
本文介绍了禁用反应形式的输入字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试遵循此处其他答案的示例,但未成功!

I already tried to follow the example of other answers from here and I did not succeed!

我创建了一个反应形式(即动态形式),我想在任何给定时间禁用某些字段.我的表单代码:

I created a reactive form (ie, dynamic) and I want to disable some fields at any given time. My form code:

this.form = this._fb.group({
  name: ['', Validators.required],
  options: this._fb.array([])
});

const control = <FormArray>this.form.controls['options'];
control.push(this._fb.group({
  value: ['']
}));

我的html:

<div class='row' formArrayName="options">
  <div *ngFor="let opt of form.controls.options.controls; let i=index">
    <div [formGroupName]="i">
      <select formArrayName="value">
        <option></option>
        <option>{{ opt.controls.value }}</option>
      </select>
    </div>
  </div>
</div>

我简化了代码以方便使用.我想禁用类型选择的字段.我尝试执行以下操作:

I reduced the code to facilitate. I want to disable the field of type select. I tried to do the following:

form = new FormGroup({
  first: new FormControl({value: '', disabled: true}, Validators.required),
});

不起作用!有人有建议吗?

not working! Does anyone have a suggestion?

推荐答案

name: [{value: '', disabled: true}, Validators.required],
name: [{value: '', disabled: this.isDisabled}, Validators.required],

this.form.controls['name'].disable();

这篇关于禁用反应形式的输入字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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