How to clear ng-select selection

前端 未结 8 1298
孤街浪徒
孤街浪徒 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: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.

提交回复
热议问题