angular 中的$event 对象包含了浏览器原生的event对象
ou can pass the $event
object as an argument when calling the function.
The $event
object contains the browser's event object:
Example
<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?
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对象的更多相关文章
- JS window对象 Navigator对象 Navigator 对象包含有关浏览器的信息,通常用于检测浏览器与操作系统的版本。
Navigator对象 Navigator 对象包含有关浏览器的信息,通常用于检测浏览器与操作系统的版本. 对象属性: 查看浏览器的名称和版本,代码如下: <script type=" ...
- angular中的jqLite所包含的jquery API
Angular本身包含了一个叫做jqLite的可兼容性库. 使用过的angular.element()方法就返回一个jqLite对象, jqLite是jQuery库的子集,它 允许Angular以跨 ...
- JavaScript基础--DOM对象(十三):(windows对象:history\location\navigator\screen\event)
DOM编程1.为什么要学习DOM(1) 通过dom编程,我们可以写出各种网页游戏(2)dom编程也是ajax的重要基础2.DOM编程介绍DOM = Document Object Model(文档对象 ...
- angular中的$http服务
$http是ng内置的一个服务.是简单的封装了浏览器原生的XMLHttpRequest对象. 写法1 $http({ method: "GET", url: 'data.json' ...
- js----Navigator对象,查看浏览器信息,Screen对象,查看屏幕信息
Navigator对象 Navigator 对象包含有关浏览器的信息,通常用于检测浏览器与操作系统的版本. 对象属性: 查看浏览器的名称和版本,代码如下: <script type=" ...
- Python进阶(3)_进程与线程中的lock(线程中互斥锁、递归锁、信号量、Event对象、队列queue)
1.同步锁 (Lock) 当全局资源(counter)被抢占的情况,问题产生的原因就是没有控制多个线程对同一资源的访问,对数据造成破坏,使得线程运行的结果不可预期.这种现象称为“线程不安全”.在开发过 ...
- Drools规则引擎-如果判断某个对象中的集合是否包含指定的值
规则引擎集合相关处理 在实际生产过程中,有很多关于集合的处理场景,比如一个Fact对象中包含有一个集合,而需要判断该集合是否包含某个值.而Drools规则引擎也提供了多种处理方式,比如通过from.c ...
- Angular中ngCookies模块介绍
1.Cookie介绍 Cookie总是保存在客户端中,按在客户端中的存储位置,可分为内存Cookie和硬盘Cookie.内存Cookie由浏览器维护,保存在内存中,浏览器关闭后就消失了,其存在时间是短 ...
- angular中的compile和link函数
angular中的compile和link函数 前言 这篇文章,我们将通过一个实例来了解 Angular 的 directives (指令)是如何处理的.Angular 是如何在 HTML 中找到这些 ...
随机推荐
- 【CZY选讲·最大子矩阵和】
题目描述 有一个n*m的矩阵,恰好改变其中一个数变成给定的常数P,使得改变后的这个矩阵的最大子矩阵最大. 数据范围 n,m<=300. 题解: ①如果没有p,那么二维矩阵和就是一维最长 ...
- 《c程序设计语言》读书笔记-5.3-指针实现strcat
#include <stdio.h> #include <math.h> #include <stdlib.h> #include <string.h> ...
- Codeforces Round #352 (Div. 2) C
C. Recycling Bottles time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- mysql外网链接
1:设置mysql的配置文件 /etc/mysql/my.cnf 找到 bind-address =127.0.0.1 将其注释掉://作用是使得不再只允许本地访问: 重启mys ...
- angular控制器controller里获取不到ng-model的值,获取为undefined
所遇问题: html:ng-model=“test”, 但是在controller里打印的$scope属性里面并未发现test,控制台打印test为undefined,页面上{{test}}却可以正常 ...
- 小红帽的画笔(NOIP模拟赛Round 7)
又到了神奇的模拟赛时间~ 真是丧~ 好吧我们来看看题目 小红帽是Pop star上最著名的人类画家,她可以将任何画出的东西变成真实的物品.赋予她这样神奇能力的正是她手上的画笔. 小红帽每次作画时,都需 ...
- python描述符的应用
使用描述符为python实现类型检测 class Typed: def __get__(self, instance, owner): print(instance) print(owner) def ...
- 使用 JavaScript 将网站后台的数据变化实时更新到前端-【知乎总结】
问: 难道只能设置定时器每隔一秒通过 Ajax 向后台请求数据来实现吗? 答: 1. nodejs的 http://socket.io 支持上述 李宏训 所说的三种方式,另外还支持 Flash Soc ...
- Selenium2+python自动化16-alert\confirm\prompt【转载】
前言 不是所有的弹出框都叫alert,在使用alert方法前,先要识别出到底是不是alert.先认清楚alert长什么样子,下次碰到了,就可以用对应方法解决. alert\confirm\prompt ...
- centos6.x一键15项系统优化(转自努力哥)
#!/bin/sh ################################################ #Author:nulige # qqinfo: # Date: -- #vers ...