0

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>

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

yeah.. sorry edited my question.. I am still seeing the issue even after setting value
can you reproduce it, because I copy paste your code and it work
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.