ou can pass the $event object as an argument when calling the function.

The $event object contains the browser's event object:

Example

<div ng-app="myApp" ng-controller="myCtrl">

<h1 ng-mousemove="myFunc($event)">Mouse Over Me!</h1>

<p>Coordinates: {{x + ', ' + y}}</p>

</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
    $scope.myFunc = function(myE) {
        $scope.x = myE.clientX;
        $scope.y = myE.clientY;
    }
});
</script>

-----------------------------------------------------------------------------------

Is Injecting The $element And $event Objects An Anti-Pattern In AngularJS?

By Ben Nadel on November 24, 2015

In the "Angular Way," there is a strict separation of concerns. The Controllers aren't supposed to know anything about the DOM (Document Object Model); the Controllers simply manage the view-model and leave it up to the Directives to "glue" the view-model to the DOM. In the "Angular Way," the Directives are the only thing that should know about the DOM. And yet, you can inject the $element into your Controller constructor and you can pass the $event into your Controller using scope methods. Which begs the question: should injecting the $element and $event objects be considered an anti-pattern in AngularJS?

Personally, I lean towards Yes on this one - that it is an anti-pattern.

However, I will caveat that with saying that sometimes the simplicity of doing so (injecting $element or $event) may substantially outweigh the complexity of keeping things separate. I don't think there's anything that can't be accomplished with $watch() bindings and event-bindings inside a link() function. But, especially for one-off events, mutating the $element directly can be more straightforward than worrying about $watch() bindings.

A good example of that might be the native Form Controller that ships with AngularJS. In the Form Controller, the $element injectable is used to add various pristine, dirty, and other state-indicating classes onto the Form element in response to method invocation (ex, form.$setDirty()). Could each of these classes be added or removed by a $watch() binding in a link() function that was observing the changes in the Form Controller's view-model? Probably. But, it may be hard to argue that such a strict separation would make the code more peformant, easier to reason about, and easier to maintain.

So, personally, I think you should avoid injecting the $element object into the Controller constructor or passing the $event object into a Controller method. I think doing so blurs the lines and breaks-down the strict separation of concerns outlined in the "Angular Way." But, as Morpheus said about rules, "some of them can be bent; others can be broken." Just make sure you're operating based on educated decisions.

angular 中的$event 对象包含了浏览器原生的event对象的更多相关文章

  1. JS window对象 Navigator对象 Navigator 对象包含有关浏览器的信息,通常用于检测浏览器与操作系统的版本。

    Navigator对象 Navigator 对象包含有关浏览器的信息,通常用于检测浏览器与操作系统的版本. 对象属性: 查看浏览器的名称和版本,代码如下: <script type=" ...

  2. angular中的jqLite所包含的jquery API

    Angular本身包含了一个叫做jqLite的可兼容性库. 使用过的angular.element()方法就返回一个jqLite对象,  jqLite是jQuery库的子集,它 允许Angular以跨 ...

  3. JavaScript基础--DOM对象(十三):(windows对象:history\location\navigator\screen\event)

    DOM编程1.为什么要学习DOM(1) 通过dom编程,我们可以写出各种网页游戏(2)dom编程也是ajax的重要基础2.DOM编程介绍DOM = Document Object Model(文档对象 ...

  4. angular中的$http服务

    $http是ng内置的一个服务.是简单的封装了浏览器原生的XMLHttpRequest对象. 写法1 $http({ method: "GET", url: 'data.json' ...

  5. js----Navigator对象,查看浏览器信息,Screen对象,查看屏幕信息

    Navigator对象 Navigator 对象包含有关浏览器的信息,通常用于检测浏览器与操作系统的版本. 对象属性: 查看浏览器的名称和版本,代码如下: <script type=" ...

  6. Python进阶(3)_进程与线程中的lock(线程中互斥锁、递归锁、信号量、Event对象、队列queue)

    1.同步锁 (Lock) 当全局资源(counter)被抢占的情况,问题产生的原因就是没有控制多个线程对同一资源的访问,对数据造成破坏,使得线程运行的结果不可预期.这种现象称为“线程不安全”.在开发过 ...

  7. Drools规则引擎-如果判断某个对象中的集合是否包含指定的值

    规则引擎集合相关处理 在实际生产过程中,有很多关于集合的处理场景,比如一个Fact对象中包含有一个集合,而需要判断该集合是否包含某个值.而Drools规则引擎也提供了多种处理方式,比如通过from.c ...

  8. Angular中ngCookies模块介绍

    1.Cookie介绍 Cookie总是保存在客户端中,按在客户端中的存储位置,可分为内存Cookie和硬盘Cookie.内存Cookie由浏览器维护,保存在内存中,浏览器关闭后就消失了,其存在时间是短 ...

  9. angular中的compile和link函数

    angular中的compile和link函数 前言 这篇文章,我们将通过一个实例来了解 Angular 的 directives (指令)是如何处理的.Angular 是如何在 HTML 中找到这些 ...

随机推荐

  1. Gcd反应堆 (pgcd)

    Gcd反应堆 (pgcd) 题目描述 不知什么时候起,TA突然对gcd产生了浓厚的兴趣,于是他为此编写了个程序,输入分别不大于m,n (1<m,n<=10^7)的两个数,就能得出gcd(m ...

  2. jquery对中文进行base64加密,后台用java进行base64解密

    项目中遇到将中文从前台传到后台过程中,出现乱码,一番尝试之后,均是乱码,然后尝试在js代码中先进行base64加密,然后在Java中再进行解密,完美的解决了乱码问题,步骤如下 一,html页面引入jQ ...

  3. 《c程序设计语言》读书笔记-删除字符串中匹配的字符

    #include <stdio.h> #include <string.h> #define Num 10 int main() { int c,i,j = 0,m,n = 0 ...

  4. 怎么用SecureCRT这个工具把linux服务器的压缩文件下载到本地的一个路径。

    依次按上图中所示的突变,进入sftp的命令界面.输入help命令:即:sftp>help得到如下的截图. 比较重要的命令有:cd----查询服务器端的路径 lcd---查询本地的地址 pwd:服 ...

  5. Linux Mint---shutter截图软件

    shutter 可以说是linux下最好的截图软件了,默认安装好后不能编辑截图,解决方法如下: 1-关闭shutter 2-sudo apt-get install shutter libgoo-ca ...

  6. eclipse 调试(debug) burpsuite 插件(Extender)

    demo: https://github.com/src-kun/transparent-cap/tree/master/burpsuite 1.打开demo项目: 2.右键项目点击Configure ...

  7. 在vc6里头文件sys/timeb.h里struct timeb各变量的具体含义?

    timeb的定义:struct _timeb{         time_t time;          unsigned short millitm;          short timezon ...

  8. 第二步:开发工具Eclipse安装并汉化

    打开下载官网:www.eclipse.org.点击下载(download英文)然后就是安装步骤了,还是一样一直的点击下一步,默认安装到C盘.如下图: 汉化步骤: 1.打开www.eclipse.org ...

  9. aliyun

    阿里云启动不了网站 1  将网站的目录属性-安全中加入IUSER_计算机名字的访问权限     和  加入NER SERVICE的访问权限 2 IIS打开网站属性--目录--执行权限改为顺脚本 3  ...

  10. UVA 1025 A Spy in the Metro 【DAG上DP/逆推/三维标记数组+二维状态数组】

    Secret agent Maria was sent to Algorithms City to carry out an especially dangerous mission. After s ...