将事件从指令发送到父元素 [英] Emit event from Directive to parent element

查看:75
本文介绍了将事件从指令发送到父元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在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>

这篇关于将事件从指令发送到父元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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