How to clear ng-select selection

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

    
    
    
    

    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.
    }
    

提交回复
热议问题