Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS - Why select drop down doesn't have $event on change

I am newbie to AngularJS. I have a question, why does ng-change doesn't passes the $event?

HTML

<div ng-controller="foo">
    <select ng-model="item" ng-options="opts as opts.name for opts in sels" ng-change="lstViewChange($event)" ng-click="listViewClick($event)"></select>
</div>

Script

var myApp = angular.module('myApp', []);
angular.element(document).ready(function() {
   angular.bootstrap(document, ['myApp']);
});

function foo($scope) {
    $scope.sels = [{id: 1, name: 'a'}, {id: 2, name: 'b'}];

    $scope.lstViewChange = function($event){
        console.log('change', $event); //undefined
    }

    $scope.listViewClick = function($event){
        console.log('click', $event); //object
    }
}

Have a look at this fiddle http://jsfiddle.net/QfZZH/. Click passes a valid $event but change doesn't. Can someone explain this behavior?

like image 468
moustacheman Avatar asked Sep 01 '13 05:09

moustacheman


1 Answers

Just to state the obvious, the docs say that ngClick will pass the event, but ngChange has no such documentation.

So, what you're asking is why Angular's designers chose to do this? My thought is that ngClick fires as a result of a click, so it make sense that there would be a mouse event associated with the directive firing. ngChange on the other hand is a result of any user action that changes the model, which could be associated with all sorts of events rather than one specific event.

like image 121
Jon7 Avatar answered Nov 15 '22 18:11

Jon7