2013-09-23 32 views
7

Angular jsを使用してselect2 on-changeイベントを発生させる際に問題が発生しました。選択ドロップダウンの変更は、変更イベントがAngular jsを使用したSelect2イベント処理

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

発生しません:

は、ここに私のhtmlです。コントローラのイベントハンドラは次のとおりです。

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

これを処理するにはどうすればよいでしょうか?私はモデル値を監視するように言われました。それはうまくいくのでしょうか、それとも良い方法がありますか? NG-モデルを見て

+0

Selectセレクトは、いくつかの回避策についてこのGoogleグループのスレッドをチェックし、明らかに角度の問題があります。https://groups.google.com/forum/# !msg/angle/YLcqj43ebR0/6gmayyHGz5MJ – tymeJV

答えて

9

オルタナティブ:ビューで

:コントローラで

<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); 
+0

これは複数回呼び出され、複数のアラートが表示されます。 –

0

あなたはどのその後、角度-UIライブラリにjqueryのリスナーを追加することができます角度イベントを発生させます。このようなもの:

そしてコントローラint型の角イベントリスナーを登録します。

$scope.$on('select2:change', function (event, element) { 
     console.log('change fired by' + element.get(0).id); 
    }); 
0

私は選択をカプセル化して、リンク機能では、私はちょうどselect要素を取得し、イベントハンドラにするディレクティブを使用。

マークアップ

<select data-ng-model="tagsSelection" ui-select2="select2Options"> 
    <option value="{{user.id}}" data-ng-repeat="user in users">{{user.name}}</option> 
</select> 

Javascriptを:

link: function (scope, element, attributes) { 
    var select = element.find('select')[0]; 
    $(select).on("change", function(evt) { 
     if (evt.added) { 
      // Do something 
     } else if (evt.removed) { 
      // Do something. 
     } 
    }); 

    $scope.select2Options = { 
     multiple: true 
    } 
} 
関連する問題