首页 > 解决方案 > (更改)不适用于角度 5 中的 select2

问题描述

(change) 指令与角度 5 中的普通 select html 标记一起工作正常。但是如果我实现 select2 插件,那么 (change) 将不起作用。

这是我的html文件

<select class="select2Select" [(ngModel)]="productSearchItem" (ngModelChange)="test()">
  <option value="">Search entire store here...</option>
  <option *ngFor="let product of products" value="{{ product.pid }}">{{ product.name }}</option>
</select>

这是一个什么都不做的函数,只是在打字稿中安慰一些东西

test(){
    console.log("Hi");
}

脚本:

$(document).ready(function() {
  $('.select2Select').select2();
});

标签: angular5jquery-select2

解决方案


jquery使用生命周期钩子执行更改事件:

ngOnInit() {
  $('.select2Select').on('select2:select', function (e) {
    console.log("Hi");
  });
}

推荐阅读