2017-05-03 7 views
3

私のコードですが、$eventは未定義です。誰も私はイベントをキャプチャすることができます知っていますか?変更のイベントをキャプチャする方法は?

<!DOCTYPE html> 
<html> 
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> 

<script type="text/javascript"> 

    var app = angular.module("myApp", []); 
    app.controller("myCtrl", function($scope) { 
     $scope.alert = function ($event){ 
      alert($event); 
     } 
    }); 
</script> 

<body ng-app="myApp" ng-controller="myCtrl"> 
<div ng-init="names=['A', 'B', 'C']"> 
    <select class="form-control input-sm" ng-model="fda" ng-change="alert($event)" ng-options="value for value in names"> 
    </select> 
</div> 
</body> 

</html> 

答えて

2

ng-change(私はこれが名前を与えられた混乱であることを認識)変更イベントを処理ためのディレクティブではありません。これは期待どおりです。あなたがイベントを取得する必要がある場合はGithub source

、あなたが使用できる代わりにNGをクリックします:あなたは、this answerが簡単にGoogleの検索結果から取られていることができません

<!DOCTYPE html> 
 
<html> 
 
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> 
 

 
<script type="text/javascript"> 
 

 
    var app = angular.module("myApp", []); 
 
    app.controller("myCtrl", function($scope) { 
 
     $scope.alert = function ($event){ 
 
      alert($event); 
 
     } 
 
    }); 
 
</script> 
 

 
<body ng-app="myApp" ng-controller="myCtrl"> 
 
<div ng-init="names=['A', 'B', 'C']"> 
 
    <select class="form-control input-sm" ng-model="fda" ng-click="alert($event)" ng-options="value for value in names"> 
 
    </select> 
 
</div> 
 
</body> 
 

 
</html>

0

ng-change is not a directive for handling the change event 
(I realize that this is confusing given the name), but is actually 
instead notified when ngModelController.$setViewValue() is called 
and the value changes (because ng-change adds a listener to the 
$viewChangeListeners collection). So this is as expected. 
1

ngMouse、ng-changeはイベントオブジェクトを提供しません。しかし、別の変数を作成し、その変数に$ eventを割り当てることができます。 ng-changeで渡します。それは私の提案

<!DOCTYPE html> 
 
<html> 
 
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> 
 

 
<script type="text/javascript"> 
 

 
    var app = angular.module("myApp", []); 
 
    app.controller("myCtrl", function($scope) { 
 
     $scope.alert = function ($event){ 
 
      alert($event); 
 
     } 
 
    }); 
 
</script> 
 

 
<body ng-app="myApp" ng-controller="myCtrl"> 
 
<div ng-init="names=['A', 'B', 'C']"> 
 
    <select class="form-control input-sm" ng-click="event = $event" ng-model="fda" ng-change="alert(event)" ng-options="value for value in names"> 
 
    </select> 
 
</div> 
 
</body> 
 

 
</html>

More details

です
関連する問題