使用Angular js选择2事件处理

Ant*_*ySG 8 angularjs

我在使用Angular js激活我的select2 on-change事件时遇到了问题.

这是我的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>
Run Code Online (Sandbox Code Playgroud)

当选择下拉列表更改时,更改事件不会触发.这是我的控制器中的事件处理程序:

$scope.DoSomething = function () {
        alert('here');
        ...
}
Run Code Online (Sandbox Code Playgroud)

处理这个问题的最佳方法是什么?我被告知要对模型值进行监视.这会有用,还是有更好的方法?

Al *_*hri 9

另外通过观看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>
Run Code Online (Sandbox Code Playgroud)

在控制器中:

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