I'm trying to check which event listener to set by checking 'ontouchstart' in window ? 'touchend' : 'click'
, and then I want to add this inside a @HostListener
but I'm unable to do this because this
is not available in the document:click
section.
Can you achieve this somehow?
constructor(private _globals: GlobalVariablesService, private _elementRef: ElementRef) {
this.ns = _globals.ns;
this.deviceListener = ('ontouchstart' in window ? 'touchend' : 'click');
}
// How can I add the this.deviceListener instead of click?
@HostListener('document:click', ['$event'])
onOutsideClick(e) {
const nativeElement = this._elementRef.nativeElement;
// If the clicked element is not the component element or any of its children, close the dropdown
if (nativeElement !== e.target && !nativeElement .contains(e.target)) {
this.close();
}
}