I'm trying to setup an attribute directive to handle click and double click on the same element. Not sure how to make the connection to the template, I have it declared and I put ClickTwiceDirective in the HTML tag. Also if there is a better way to do this I'm interested. I thought it would be better to avoid click and double click on the same element but that is the requirement and what needs to be designed for (single click select, double click editable).
import {Directive, ElementRef, Output, EventEmitter, HostListener} from '@angular/core';
@Directive({
selector: '[clickTwice]'
})
export class ClickTwiceDirective {
clickedElement: number = 0;
constructor(private _elementRef : ElementRef) {
}
@Output()
public singleClick = new EventEmitter();
public doubleClick = new EventEmitter();
@HostListener('this._elementRef:click', ['$event.target'])
public onClick(targetElement: any) {
console.log('got here');
this.clickedElement++;
setTimeout(() =>{
if(this.clickedElement < 2){
this.singleClick.emit(null);
this.clickedElement = 0;
} else {
this.doubleClick.emit(null);
this.clickedElement = 0;
}
}, 300);
}
}
HTML input example of how I think it is supposed to look
<input type="text" (singleClick)='func1()' (doubleClick)="func2()" clickTwice/>