HostListener vs DOM events in Angular 4
So this question is about best practices while coding. Consider I have a component on which I want a blur event. This can be implemented in 2 different ways.
Using the Dom event:
view.html
<input (blur)="onBlur($event)"/>
component.ts
@Component({ selector: 'input-component' template: require('path to view.html') }) export class InputComponent(){ public onBlur(event: Event){ /*on blur action*/} }
Here I have put a blur event on the input and I have a callback function in my component class.
Using HostListener
component.ts
import { HostListener } from '@angular/core'; @Component({ selector: 'input-component' template: require('path to view.html') }) export class InputComponent(){ @HostListener('blur', ['$event']) onBlur(event: Event) { /*on blur action*/} }
Here I implemented the same with HostListener. I know that this is used a lot in directive class, but I have seen a lot of examples of component with HostListeners.
My question here is:
- Which is the more cleaner and better implementation?
- Is it better to stick to the DOM events or go with the angular hooks?