$ 2事件对象在Angular 2中做了什么? [英] What exactly $event object do in Angular 2?
问题描述
我有点困惑,这个事情在这里做了什么,这两个例子有什么区别?
I am bit confused what exactly $event doing here and what is the difference between this two examples
<button (click)="clicked($event)"></button>
@Component(...)
class MyComponent {
clicked(event) {
event.preventDefault();
}
}
和
<button (click)="clicked()">Click</button>
@Component(...)
class MyComponent {
clicked(event) {
}
}
推荐答案
$ event
是事件本身。
事件绑定(someevent)
允许绑定到DOM事件和 EventEmitter
事件。语法完全一样。
The event binding (someevent)
allows to bind to DOM events and to EventEmitter
events. The syntax is exactly the same.
对于DOM事件 $ event
是 MouseEvent
, KeyboardEvent
,或您收听的任何事件类型的事件值。
For DOM events $event
is the MouseEvent
, KeyboardEvent
, or the event value of the type of whatever event you listen to.
对于 EventEmitter
事件,发布的值可用作 $ event
For EventEmitter
events the emitted value is available as $event
假设这个例子 $ event
指的是发出的法拉利汽车实例:
Assuming this example $event
refers to the emitted Ferrari
car instance:
@Output() carChange:EventEmitter<Car> = new EventEmitter<Car>();
someMethod() {
this.carChange.emit(new Car({name: 'Ferrari'}));
}
如果不使用 $ event
喜欢在
(click)=clicked()
然后事件值不传递。
If you don't use $event
like in (click)="clicked()"
then the event value is not passed.
事实上,据我所知,仍然在某些浏览器中传递,但不是全部(不记得哪些)
Actually as far as I remember it is still passed in some browsers but not in all (don't remember which ones)
但是如果您使用Angulars WebWorker功能,那么如果您没有明确列出它,您的方法可能无法获得触发或发射的事件。
But if you use Angulars WebWorker feature, then your method might not get the fired or emitted event if you don't explicitely list it.
这篇关于$ 2事件对象在Angular 2中做了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!