最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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 /. Click passes a valid $event but change doesn't. Can someone explain this behavior?

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/QfZZH/. Click passes a valid $event but change doesn't. Can someone explain this behavior?

Share Improve this question edited Sep 1, 2013 at 5:34 moustacheman asked Sep 1, 2013 at 5:02 moustachemanmoustacheman 1,4644 gold badges23 silver badges47 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

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.

发布评论

评论列表(0)

  1. 暂无评论