How to use select2 in Angular 6?

痞子三分冷 提交于 2019-12-11 17:42:31

问题


I need to use "select2" in angular 6 for autocomplete. Who's ever use it before? , help me please.


回答1:


You can use ng2-select2

Angular wrapper for Select2 (ng2-select2)




回答2:


1.If you are using bootstrap then use typeahead feature of bootstrap

https://ng-bootstrap.github.io/#/components/typeahead/examples

2.If material then use autocomplete

https://material.angular.io/components/autocomplete/examples




回答3:


you can do this in Two ways

case1: with JQuery by adding an Id to select tag

<select select2 style="width:100%;" class="select2" id="symbolId" [(ngModel)]="selectedContractDetails.name">
        <option *ngFor="let symbol of service.symbols" value="{{symbol}}">{{symbol}}</option>
    </select>

In your ngAfterViewInit()

ngAfterViewInit(){
   $('#symbolId').on('change', (event) => {
       var symbolSelected= event.target.value;
       //you can use the selected value
   });
}

case2: with JQuery and select2 Official Library methods

ngAfterViewInit(){
   $('.select2').on('select2:select', (event) => {
       // Code you want to execute when you hit enter in select2 input box   
   });
}


来源:https://stackoverflow.com/questions/52831273/how-to-use-select2-in-angular-6

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!