app
app

Reputation: 207

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>

Upvotes: 0

Views: 4505

Answers (2)

Kavinda Senarathne
Kavinda Senarathne

Reputation: 2004

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

Upvotes: 1

Oleksii Pavlenko
Oleksii Pavlenko

Reputation: 1333

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

Upvotes: 1

Related Questions