Is it possible to programmatically clear the selection of an ng-select dropdown? I'm wanting the equivalent behaviour of clicking the clear icon, but triggered programmatically.
I was expecting a clear()
method or something similar, but the documented API doesn't have anything along those lines.
This is my dropdown code:
<ng-select class="ng-select-wrap" [searchFn]="multiTermSearch" [items]="calculationOptions" placeholder="Please select..." name="calculation" #calculationValue="ngModel" [(ngModel)]="selectedCalculation"> </ng-select>
In your stylesheet add display: none; to the . ng-clear-wrapper selector.
if 'clearable' property is set to false, and clearOnBackspace is set to true, user can remove single item using backspace button. if (this.filterValue || (!multiple && ! this.clearable) || ! this.clearOnBackspace || ! this.hasValue) { template example: <ng-select> (add)=... (
The ng-selected directive sets the selected attribute of an <option> element in a <select> list. The option will be selected if the expression inside the ng-selected attribute returns true. The ng-selected directive is necessary to be able to shift the value between true and false .
Go to 'ng-select component and search something you get some option regarding it. ' Click OR select the option. And after the selection the value Cursor focus out from the component.
Here is solution from comment:
// Access ng-select @ViewChild(NgSelectComponent) ngSelectComponent: NgSelectComponent; // Call to clear this.ngSelectComponent.handleClearClick();
Note that handleClearClick
isn't exposed in docs as public api method however as Tim mentioned it's public method so it's possible to call it.
I was looking for the same but for templates to invoke it outside ng-select
. So, the below worked fine for me following the accepted answer. ^_^
<ng-select class="ng-select-wrap" [searchFn]="multiTermSearch" [items]="calculationOptions" placeholder="Please select..." name="calculation" (clear)="resetCalculations();" [(ngModel)]="selectedCalculation" #selectDropdown> </ng-select> <input type="button" (click)="selectDropdown.handleClearClick()">
This also makes sure the
resetCalculations()
being called.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With