Search code examples
angularangular-directive

Emit event from Directive to parent element


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?


Solution

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

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