How can I make ng-select disabled? I tried [disabled] property, but it does not work here. Also tried to make it disabled using form control, but nothing works.
3 Answers
You can disable/enable a form control when you initialize it by setting disabled to true
creds.push(this.fb.group({
fruitType: this.fb.control({value: 'Apple', disabled: true})
}));
To disable/enable the control later dynamically in your component. You can do so by calling the disable/enable methods for that particular form control.
// To enable form control
fruitType.enable();
// To disable form control
fruitType.disable();
2 Comments
If you are looking to disable ng-select in html using any dynamic condition then you can use [readonly] property.
<ng-select
formControlName="myControl"
[readonly]="condition_resolving_to_true_or_false"
</ng-select>
2 Comments
you can set the value and the disabled state directly by pass an object same @nash11 example but without using this.fb.control it will be done internally
addFruits() {
const creds = this.form.controls.fruits as FormArray;
creds.push(this.fb.group({
fruitType: { value: 'Apple', disabled: true } // 👈
}));
}
in case you want to pass a validator you can use an array and the initial value is an object
addFruits() {
const creds = this.form.controls.fruits as FormArray;
creds.push(this.fb.group({
fruitType: [{ value: 'Apple', disabled: true },Validators.required] // 👈
}));
}
ng-disabled?