使用 Angular js 处理 Select2 事件 [英] Select2 event handling with Angular js

查看:30
本文介绍了使用 Angular js 处理 Select2 事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 Angular js 触发 select2 on-change 事件时遇到问题.

I'm having an issue getting my select2 on-change event to fire using Angular js.

这是我的 html:

<select ui-select2="{allowClear:true}" data-placeholder="Select a Department" ng-change="DoSomething()" class="input-max" ng-model="l.DepartmentID">
    <option value=""></option>
    <option ng-repeat="d in l.LineLookups.Departments" value="{{d.DepartmentID}}">{{d.Type}}</option> </select>

When the select dropdown changes, the change event doesn't fire.这是我的控制器中的事件处理程序:

When the select dropdown changes, the change event doesn't fire. Here's my event handler in my controller:

$scope.DoSomething = function () {
        alert('here');
        ...
}

处理这个问题的最佳方法是什么?有人告诉我要注意模型值.这会奏效,还是有更好的方法?

What's the best way to handle this? I've been told to set a watch on the model value. Will that work, or is there a better way?

推荐答案

观看 ng-model 的替代方案:

Alternative by watching ng-model:

在视图中:

<select ui-select2="{allowClear:true}" data-placeholder="Select a Department" class="input-max" ng-model="l.DepartmentID">
<option value=""></option>
<option ng-repeat="d in l.LineLookups.Departments" value="{{d.DepartmentID}}">{{d.Type}}</option>
</select>

在控制器中:

$scope.$watch('l.DepartmentID', function (newVal, oldVal) {
    if (oldVal == newVal) return;
    alert('here');
}, true);

这篇关于使用 Angular js 处理 Select2 事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆