AngularJS - 为什么选择下拉菜单没有变化的 $event [英] AngularJS - Why select drop down doesn't have $event on change

查看:25
本文介绍了AngularJS - 为什么选择下拉菜单没有变化的 $event的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 AngularJS 的新手.我有一个问题,为什么 ng-change 不通过 $event?

HTML

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

脚本

var myApp = angular.module('myApp', []);angular.element(document).ready(function() {angular.bootstrap(document, ['myApp']);});函数 foo($scope) {$scope.sels = [{id: 1, name: 'a'}, {id: 2, name: 'b'}];$scope.lstViewChange = 函数($event){console.log('change', $event);//不明确的}$scope.listViewClick = function($event){console.log('click', $event);//目的}}

看看这个小提琴 http://jsfiddle.net/QfZZH/.Click 传递有效的 $event 但 change 不会.有人可以解释这种行为吗?

解决方案

只是为了说明显而易见的事情,文档说 ngClick 将传递事件,但是 ngChange 有没有这样的文件.

那么,您要问的是为什么 Angular 的设计师选择这样做?我的想法是 ngClick 作为点击的结果触发,所以有一个与指令触发相关的鼠标事件是有道理的.另一方面,ngChange 是任何改变模型的用户操作的结果,它可能与各种事件而不是一个特定事件相关联.

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?

解决方案

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.

这篇关于AngularJS - 为什么选择下拉菜单没有变化的 $event的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆