简体   繁体   English

ng-select 禁用表单控件在反应形式角度不起作用

[英]ng-select disable form control not working in reactive form angular

I was trying to setvalue to my form and would like to disable the form.我试图为我的表单设置值并想禁用该表单。

Somehow if I setvalue to form it is working fine but as soon I write disable method my ng-select value is going out.不知何故,如果我设置值来形成它工作正常,但是一旦我编写禁用方法,我的 ng-select 值就会消失。

Have anyone faced same issue while disabling the ng-select with values?有没有人在禁用带有值的 ng-select 时遇到同样的问题?

 form = new FormGroup({ code: new FormControl() }); values = [{ value: "0" label: "test1" }, { value: "1" label: "test2" } ] //I am setting values using setvalue and disbling after wards this.form.controls['code'].setValue('0') this.form.disable();
 <ng-select formControlName="code" [items]="values" bindValue="value" labelForId="code" [clearable]="false" [selectOnTab]="true" placeholder="Select one" data-e2e-id="code"> </ng-select>

Are you sure that you change value?你确定你改变了价值?

Method to set value:设置值的方法:

Methods using setValue()使用 setValue() 的方法

this.form.get("code").setValue('0');
this.form.controls["code"].setValue('0');

Methods using patchValue()使用 patchValue() 的方法

this.form.get("code").patchValue('0');
this.form.controls['code'].patchValue('0');
this.form.patchValue({"code": '0'});

In you case could be the problem that form updates only after applying disablibg and you doesn`t see that you update filed on a wrong way在您的情况下可能是表单仅在应用 disablibg 后更新的问题,并且您没有看到您以错误的方式更新提交

You can disable/enable a form control when you initialize it by setting disabled to true as follows您可以在初始化时禁用/启用表单控件,方法是将disabled设置为true ,如下所示

users.push(this.fb.group({
  userType: this.fb.control({value: 'Admin', disabled: true})
}));

You can do so by calling the disable/enable methods for that particular form control to perform it dynamically.您可以通过调用该特定表单控件的禁用/启用方法来动态执行它。

// To enable form control
userType.enable();

// To disable form control
userType.disable();

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

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