directive click outside angular 6

前端 未结 4 2166
广开言路
广开言路 2021-02-19 04:01

I upgraded my Angular from 4 to 6, and consequently had a problem with my click-off policy, it stopped working on all components.

my directive:



        
4条回答
  •  时光说笑
    2021-02-19 04:13

    view:

    component:

    export class SomeClass {
      @ViewChild("insideElement") insideElement;
      @HostListener('document:click', ['$event.target'])
    
      public onClick(targetElement) {
        const clickedInside = this.insideElement.nativeElement.contains(targetElement);
        if (!clickedInside) {
          console.log('outside clicked');
        }
      }
    }
    

提交回复
热议问题