简体   繁体   中英

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.

Have anyone faced same issue while disabling the ng-select with values?

 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()

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

Methods using 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

You can disable/enable a form control when you initialize it by setting disabled to true as follows

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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