4

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.

Stackblitz example

1
  • Did you try ng-disabled? Commented Jul 23, 2019 at 11:20

3 Answers 3

6

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();
Sign up to request clarification or add additional context in comments.

2 Comments

thanks, I forgot about this.fb.control to make it disabled, I just initialized it as a simple object.
@nash11 your answer is totally valid but I just add my answer as an extra information 👍 even it 's not mention in the documentation as it 's possible to pass an object for control initial value 😅😅
3

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

Getting Error : Can't bind to 'readOnly' since it isn't a known property of 'ng-select'.
First check the your letters case, I have written all letters small not like you having having camelcase notation with O capital. Downvote any answer after carefully observing. @Dijo
2

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] // 👈
    }));
  }

demo 🔥🔥

initialize form control object by FormBuilder

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.