25

We are using ng-select in a project and I'm facing the problem that I can't disable the ng-select window. Is it possible to disable it with native code or do I need to find some custom solution?

  <ng-select 
    #changeowner
    class="custom-owner"
    [placeholder]="leagueOwner.full_name"
    [clearSearchOnAdd]="true"
    (change)="changeLeagueOwner($event)"
    [clearable]="false"
    [virtualScroll]="true"
    [items]="adminLeagueMembers"
    (scrollToEnd)="onAdminLoadScrollEnd()"
    bindLabel="full_name">
</ng-select>
Bogdan Tushevskyi
  • 712
  • 2
  • 12
  • 25

11 Answers11

20

As stated in other responses you can use the option

[disabled]="booleanValue"

However you will need a formControlName value set as well as described here.

You would get:

<!-- test-component.html -->
<ng-select [disabled]="myDisabledCondition" formControlName="myFormControl" 
...>
</ng-select>
Nasta
  • 819
  • 8
  • 21
13

With latest angular versions disabled attribute doesn't live well with formControlName. One way to disable ng-select with formControlName is to show readonly textbox instead of disabled dropdown:

<div *ngIf="!disabledCondition">
  <ng-select 
    #changeowner
    formControlName="owner"
    class="custom-owner"
    [placeholder]="leagueOwner.full_name"
    [clearSearchOnAdd]="true"
    (change)="changeLeagueOwner($event)"
    [clearable]="false"
    [virtualScroll]="true"
    [items]="adminLeagueMembers"
    (scrollToEnd)="onAdminLoadScrollEnd()"
    bindLabel="full_name">
  </ng-select>
</div>
<div *ngIf="disabledCondition">      
  <input formControlName="owner" class="form-control" type="text" readonly>
</div>
Erikas Pliauksta
  • 1,402
  • 1
  • 14
  • 22
13

If you are using the Angular reactive form and trying to disable the ng-select using the following code then sorry it will not work in some case. If you are using formControlName then it will not work but work in [(ngModel)] case.

[disabled] = true

The best way to do this is by using reactive form control.

this.createReactiveform.controls['some_key'].disable(true);
Inamur Rahman
  • 2,913
  • 1
  • 27
  • 29
11

If you are using reactForms, the correct way to disable inputs is to use:

 formGroup.get('controlName').disable()
Muhammad Dyas Yaskur
  • 6,914
  • 10
  • 48
  • 73
Renan Lima
  • 111
  • 1
  • 2
5

You should use [readonly] input

https://github.com/ng-select/ng-select#api

[readonly]  boolean     false   no  Set ng-select as readonly. Mostly used with reactive forms.
marcosalpereira
  • 319
  • 3
  • 5
3

use [disabled]="true"

<ng-select 
    [disabled]="true"
    #changeowner
    class="custom-owner"
    [placeholder]="leagueOwner.full_name"
    [clearSearchOnAdd]="true"
    (change)="changeLeagueOwner($event)"
    [clearable]="false"
    [virtualScroll]="true"
    [items]="adminLeagueMembers"
    (scrollToEnd)="onAdminLoadScrollEnd()"
    bindLabel="full_name">
</ng-select>
Krishna Rathore
  • 9,389
  • 5
  • 24
  • 48
3

If you are using reactiveForms, the correct way to disable inputs is to use:

 formGroup.get('controlName').disable()

in case you need to empty the formcontrol

 formGroup.get('controlName').setValue("");
Akitha_MJ
  • 3,882
  • 25
  • 20
3

adding [disabled]="true" did not work for me But setting [readOnly] to true did. Based on documentation that is a way to go with reactive forms:

[readonly] boolean false no Set ng-select as readonly. Mostly used with reactive forms.

2

ng-select has native method which not include in official documentation - setDisabledState(boolean) So, i used

@ViewChild ('changeowner') changeOwnerRef: ElementRef;

and can use this method like this:

(<any>this.changeOwnerRef).setDisabledState(true);
Bogdan Tushevskyi
  • 712
  • 2
  • 12
  • 25
1

use disable attribute:

<ng-select 
   [disabled]="true">
</ng-select>

If you get Can't bind to 'disabled' since it isn't a known property of 'ng-select'. You have to reference FormsModule:

@NgModule({
   imports: [FormsModule, NgSelectModule]
})
Marek Pavelek
  • 1,097
  • 12
  • 12
0

In some versions of Angular (e.g., version 13), the [disabled] attribute may not work correctly with the component. To disable the component in Angular version 13, you can use an alternative approach.

First, create a CSS class that disables the pointer-events:

.ng-select-disabled {
  pointer-events: none;
}

Next, in your component or template file, apply this class based on the value of the readOnly variable for example:

<ng-select 
    #changeowner
    class="custom-owner"
    [placeholder]="leagueOwner.full_name"
    [clearSearchOnAdd]="true"
    (change)="changeLeagueOwner($event)"
    [clearable]="false"
    [virtualScroll]="true"
    [items]="adminLeagueMembers"
    (scrollToEnd)="onAdminLoadScrollEnd()"
    bindLabel="full_name"
    [class.ng-select-disabled]="readOnly" <!-- Bind to the readOnly variable -->
    >
</ng-select>