本文介绍了将事件从指令发送到父元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在Angular 2应用的HTML模板中有一个元素.我向它添加了一条指令:
I have an element in HTML template of an Angular 2 app. I add a directive to it:
<div myCustomDirective>HELLO</div>
我希望每当将鼠标悬停在div
上时,都应更改div
内的文本,但这需要从Directive
(mouseover)
事件中完成.
I want that whenever I hover over the div
the text inside the div
should be changed, but it needs to be done from Directive
(mouseover)
event.
如何从Directive
发出事件并将其捕获到父元素中?
How to emit an event from a Directive
and capture it inside a parent element?
推荐答案
如果myCustomDirective
的输出为@Output() someEvent:EventEmitter = new EventEmitter();
,则可以使用
If myCustomDirective
has an output @Output() someEvent:EventEmitter = new EventEmitter();
then you can use
<div myCustomDirective (someEvent)="callSomethingOnParent($event)">HELLO</div>
这篇关于将事件从指令发送到父元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!