How to detect click outside of an element in Angular?

前端 未结 7 763
死守一世寂寞
死守一世寂寞 2020-12-29 10:06

This div will be dynamically shown on the page as an east panel when the open panel button is clicked. The bool showEastPanel variable

7条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-29 10:55

    you can do something like this

      @HostListener('document:mousedown', ['$event'])
      onGlobalClick(event): void {
         if (!this.elementRef.nativeElement.contains(event.target)) {
            // clicked outside => close dropdown list
         this.isOpen = false;
         }
      }
    

    and use *ngIf=isOpen for the panel

提交回复
热议问题