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

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

问题描述

我是新手,AngularJS。我有一个问题,为什么 NG-变化不传递$事件?

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>

剧本

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

看一看这个小提琴 http://jsfiddle.net/QfZZH/ 点击传递有效的$事件,但更改没有。有人可以解释这种现象?

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

推荐答案

只是陈述明显,文档说 ngClick 将传递事件,但 ngChange 有没有这样的文件。

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

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

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 - 为什么选择下拉菜单上没有变化$事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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