How to clear ng-select selection

前端 未结 8 1257
孤街浪徒
孤街浪徒 2021-02-19 07:21

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

相关标签:
8条回答
  • 2021-02-19 07:40

    The best answer is for clearing an input field on a specific action. I also use this code in my project according to my requirement.

    In HTML write this code:

    <ng-select [items]="products" #ngSelectComponent
               bindLabel="name"
               placeholder="Enter Product Name /SKU/ Scan Barcode"
               appendTo="body"
               name="selectedProduct"
               id="selectedProduct"
               (change)="productChange($event)"
               [multiple]="false"
               ([ngModel])="selectedProduct" >
    
    <input type="button" (click)="clearValue()">
    

    In .TS file add a ViewChild for this control so that if your screen consists of multiple ngSelect elements then you can clear a specific ngSelect value.

    @ViewChild('ngSelectComponent') ngSelectComponent: NgSelectComponent;
    
    clearValue(){
      this.ngSelectComponent.clearModel(); 
      // This line will clear the value of ngSelect control. 
      // You can write code according to your requirement.
    }
    
    0 讨论(0)
  • 2021-02-19 07:43

    Clearing the selection can be achieved by simply setting the ngModel value to null. In the case of the above example:

    this.selectedCalculation = null;
    

    This isn't exactly the same as clicking the clear icon, as it doesn't trigger the (clear) output event, but it was sufficient for my needs.

    0 讨论(0)
  • 2021-02-19 07:44

    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.

    0 讨论(0)
  • 2021-02-19 07:48

    Agree with @tim and @AlexOnozor, I have successfully used 'selectedCalculation' as 'string', 'string[]', 'object[]' with Reactive Forms ( and as 'string' with ngModel ) and your suggested method worked smoothly. I also tried using 'handleClearClick' but failed. Will update if i find my way through it.

    So, this.selectedCalculation = '' or this.selectedCalculation= [] (for multipleSelect = true) should work.

    0 讨论(0)
  • 2021-02-19 07:53

    Even thought @Buczkowski answer clears ng-select, it also does a focus on input, which isn't needed for all cases. So i used other method: clearModel

      // Access ng-select
      @ViewChild(NgSelectComponent) ngSelectComponent: NgSelectComponent;
    
      // Call to clear
      this.ngSelectComponent.clearModel();
    

    So if you need to just clear input, use clearModel method. Else if focus and clear is needed, use handleClearClick.

    0 讨论(0)
  • 2021-02-19 07:59

    Like @tim answer is correct but I will prefer you set as an empty array instead of null just to save any loop break.

    this.selectedCalculation = [];
    
    0 讨论(0)
提交回复
热议问题