Emit event from Directive to parent element

raju picture raju · Jun 22, 2016 · Viewed 36.7k times · Source

I have an element in HTML template of an Angular 2 app. I add a directive to it:

<div myCustomDirective>HELLO</div>

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.

How to emit an event from a Directive and capture it inside a parent element?

Answer

G&#252;nter Z&#246;chbauer picture Günter Zöchbauer · Jun 22, 2016

If myCustomDirective has an output @Output() someEvent:EventEmitter = new EventEmitter(); then you can use

<div myCustomDirective (someEvent)="callSomethingOnParent($event)">HELLO</div>