Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@HostListener OnClick for outside click does not work in firefox

@HostListener OnClick does not work in firefox. I tried onClick, onclick and onGlobalClick. They all work in chrome but no one in firefox. Here is my code:

constructor(private elRef: ElementRef, private renderer: Renderer2) { }

@Output() offClick = new EventEmitter();

@HostListener('document:click', ['$event.path'])
public onGlobalClick(targetElementPath: Array<any>) {
  const elementRefInPath = targetElementPath.find(e => e === this.elRef.nativeElement);
  if (!elementRefInPath) {
    this.offClick.emit(1);
  }
}
like image 685
Dato Gogshelidze Avatar asked Sep 01 '17 07:09

Dato Gogshelidze


1 Answers

Here is what I tested in all browsers and it works:

 @HostListener('document:click', ['$event', '$event.target'])
    onClick(event: MouseEvent, targetElement: HTMLElement): void {
        if (!targetElement) {
            return;
        }
        const clickedInside = this.elementRef.nativeElement.contains(targetElement);
        if (!clickedInside) {
            this.clickOutside.emit(event);
        }
    }
like image 177
Vega Avatar answered Oct 09 '22 08:10

Vega