In Angular2, how can I target an element within the HostListener decorator?
@HostListener(\'dragstart\', [\'$event\'])
onDragStart(ev:Event) {
co
I think better way for global listener is @hostliterner but if you want to target some element you can do like this
<div (event)="onEvent($e)"></div>
in your angular component
onEvent($e) { //do something ... }
You can also target any element without @hostlistener and can add necessary events by the following way
import { AfterViewInit, Component, ElementRef} from '@angular/core';
constructor(private elementRef:ElementRef) {}
ngAfterViewInit() {
this.elementRef.nativeElement.querySelector('my-element')
.addEventListener('click', this.onClick.bind(this));
}
onClick(event) {
console.log(event);
}