177

How can I detect clicks outside a component in Angular?

Mark Amery
  • 143,130
  • 81
  • 406
  • 459
AMagyar
  • 5,108
  • 3
  • 13
  • 17

12 Answers12

297
import { Component, ElementRef, HostListener, Input } from '@angular/core';

@Component({
  selector: 'selector',
  template: `
    <div>
      {{text}}
    </div>
  `
})
export class AnotherComponent {
  public text: String;

  @HostListener('document:click', ['$event'])
  clickout(event) {
    if(this.eRef.nativeElement.contains(event.target)) {
      this.text = "clicked inside";
    } else {
      this.text = "clicked outside";
    }
  }

  constructor(private eRef: ElementRef) {
    this.text = 'no clicks yet';
  }
}

A working example - click here

Shashank Vivek
  • 16,888
  • 8
  • 62
  • 104
AMagyar
  • 5,108
  • 3
  • 13
  • 17
  • 25
    This doesn't work when there is an element controlled by an ngIf inside the trigger element, since the ngIf removing the element from the DOM happens before the click event: http://plnkr.co/edit/spctsLxkFCxNqLtfzE5q?p=preview – J. Frankenstein Oct 09 '17 at 23:23
  • does it work on a component that created dynamiclly via : const factory = this.resolver.resolveComponentFactory(MyComponent); const elem = this.vcr.createComponent(factory); – Avi Moraly Feb 12 '18 at 17:19
  • 2
    A nice article on this topic: https://christianliebel.com/2016/05/angular-2-a-simple-click-outside-directive/ – Miguel Lara Jan 24 '19 at 12:32
87

An alternative to AMagyar's answer. This version works when you click on element that gets removed from the DOM with an ngIf.

http://plnkr.co/edit/4mrn4GjM95uvSbQtxrAS?p=preview

  private wasInside = false;

  @HostListener('click')
  clickInside() {
    this.text = "clicked inside";
    this.wasInside = true;
  }

  @HostListener('document:click')
  clickout() {
    if (!this.wasInside) {
      this.text = "clicked outside";
    }
    this.wasInside = false;
  }
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
J. Frankenstein
  • 1,673
  • 14
  • 24
71

Binding to a document click through @Hostlistener is costly. It can and will have a visible performance impact if you overuse it (for example, when building a custom dropdown component and you have multiple instances created in a form).

I suggest adding a @Hostlistener() to the document click event only once inside your main app component. The event should push the value of the clicked target element inside a public subject stored in a global utility service.

@Component({
  selector: 'app-root',
  template: '<router-outlet></router-outlet>'
})
export class AppComponent {

  constructor(private utilitiesService: UtilitiesService) {}

  @HostListener('document:click', ['$event'])
  documentClick(event: any): void {
    this.utilitiesService.documentClickedTarget.next(event.target)
  }
}

@Injectable({ providedIn: 'root' })
export class UtilitiesService {
   documentClickedTarget: Subject<HTMLElement> = new Subject<HTMLElement>()
}

Whoever is interested for the clicked target element should subscribe to the public subject of our utilities service and unsubscribe when the component is destroyed.

export class AnotherComponent implements OnInit {

  @ViewChild('somePopup', { read: ElementRef, static: false }) somePopup: ElementRef

  constructor(private utilitiesService: UtilitiesService) { }

  ngOnInit() {
      this.utilitiesService.documentClickedTarget
           .subscribe(target => this.documentClickListener(target))
  }

  documentClickListener(target: any): void {
     if (this.somePopup.nativeElement.contains(target))
        // Clicked inside
     else
        // Clicked outside
  }
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ginalx
  • 1,905
  • 1
  • 15
  • 19
  • 7
    I think that this one should become the accepted answer as it allows for many optimizations: like in [this example](https://stackoverflow.com/a/60014879/702951) – edoardo849 Feb 01 '20 at 07:26
  • 3
    @lampshade Correct. I talked about this. Read the answer again. I leave the unsubscribe implementation to your style (takeUntil(), Subscription.add()). Don't forget to unsubscribe! – ginalx Jun 12 '20 at 08:15
  • 1
    Is there a way for the HostListener to only be active when there are subscribers interested in the events? – David Jan 27 '22 at 11:56
  • This triggers tick() twice per click for me even when nothing is subscribed to it. I found https://stackoverflow.com/a/51152404/3266845 which uses Renderer2 and it also exhibits the same behavior, but with that one I wrapped it around runOutsideAngular(). Now my dropdown components don't trigger wasted change detections. https://medium.com/claritydesignsystem/four-ways-of-listening-to-dom-events-in-angular-part-3-renderer2-listen-14c6fe052b59 – Kerry Johnson Nov 23 '22 at 20:14
14

Improving J. Frankenstein's answer:

  @HostListener('click')
  clickInside($event) {
    this.text = "clicked inside";
    $event.stopPropagation();
  }

  @HostListener('document:click')
  clickOutside() {
      this.text = "clicked outside";
  }
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
John Libes
  • 363
  • 3
  • 11
7

The previous answers are correct, but what if you are doing a heavy process after losing the focus from the relevant component? For that, I came with a solution with two flags where the focus out event process will only take place when losing the focus from relevant component only.

isFocusInsideComponent = false;
isComponentClicked = false;

@HostListener('click')
clickInside() {
    this.isFocusInsideComponent = true;
    this.isComponentClicked = true;
}

@HostListener('document:click')
clickout() {
    if (!this.isFocusInsideComponent && this.isComponentClicked) {
        // Do the heavy processing

        this.isComponentClicked = false;
    }
    this.isFocusInsideComponent = false;
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Rishanthakumar
  • 871
  • 10
  • 20
5

ginalx's answer should be set as the default one imo: this method allows for many optimizations.

The problem

Say that we have a list of items and on every item we want to include a menu that needs to be toggled. We include a toggle on a button that listens for a click event on itself (click)="toggle()", but we also want to toggle the menu whenever the user clicks outside of it. If the list of items grows and we attach a @HostListener('document:click') on every menu, then every menu loaded within the item will start listening for the click on the entire document, even when the menu is toggled off. Besides the obvious performance issues, this is unnecessary.

You can, for example, subscribe whenever the popup gets toggled via a click and start listening for "outside clicks" only then.


isActive: boolean = false;

// to prevent memory leaks and improve efficiency, the menu
// gets loaded only when the toggle gets clicked
private _toggleMenuSubject$: BehaviorSubject<boolean>;
private _toggleMenu$: Observable<boolean>;

private _toggleMenuSub: Subscription;
private _clickSub: Subscription = null;


constructor(
 ...
 private _utilitiesService: UtilitiesService,
 private _elementRef: ElementRef,
){
 ...
 this._toggleMenuSubject$ = new BehaviorSubject(false);
 this._toggleMenu$ = this._toggleMenuSubject$.asObservable();

}

ngOnInit() {
 this._toggleMenuSub = this._toggleMenu$.pipe(
      tap(isActive => {
        logger.debug('Label Menu is active', isActive)
        this.isActive = isActive;

        // subscribe to the click event only if the menu is Active
        // otherwise unsubscribe and save memory
        if(isActive === true){
          this._clickSub = this._utilitiesService.documentClickedTarget
           .subscribe(target => this._documentClickListener(target));
        }else if(isActive === false && this._clickSub !== null){
          this._clickSub.unsubscribe();
        }

      }),
      // other observable logic
      ...
      ).subscribe();
}

toggle() {
    this._toggleMenuSubject$.next(!this.isActive);
}

private _documentClickListener(targetElement: HTMLElement): void {
    const clickedInside = this._elementRef.nativeElement.contains(targetElement);
    if (!clickedInside) {
      this._toggleMenuSubject$.next(false);
    }    
 }

ngOnDestroy(){
 this._toggleMenuSub.unsubscribe();
}

And, in *.component.html:


<button (click)="toggle()">Toggle the menu</button>

edoardo849
  • 143
  • 1
  • 2
  • 8
  • 2
    While I agree with the way you think, I'd suggest not stuffing all logic in a `tap` operator. Instead, use `skipWhile(() => !this.isActive), switchMap(() => this._utilitiesService.documentClickedTarget), filter((target) => !this._elementRef.nativeElement.contains(target)), tap(() => this._toggleMenuSubject$.next(false))`. This way you utilize way more of RxJs and skip some subscriptions. – Gizrah Mar 26 '20 at 12:35
1

You can use the clickOutside() method from the ng-click-outside package; it offers a directive "for handling click events outside an element".

NB: This package is currently deprecated. See https://github.com/arkon/ng-sidebar/issues/229 for more info.

cigien
  • 57,834
  • 11
  • 73
  • 112
James Bond
  • 35
  • 2
1

Another possible solution using event.stopPropagation():

  1. define a click listener on the top most parent component which clears the click-inside variable
  2. define a click listener on the child component which first calls the event.stopPropagation() and then sets the click-inside variable
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
HoseinGhanbari
  • 1,058
  • 1
  • 11
  • 23
1

Alternative to MVP, you only need to watch for Event

@HostListener('focusout', ['$event'])
  protected onFocusOut(event: FocusEvent): void {
    console.log(
      'click away from component? :',
      event.currentTarget && event.relatedTarget
    );
  }
  • What do you mean by "[MVP](https://en.wikipedia.org/wiki/MVP_(disambiguation)#Computing)"? [Model–view–presenter](https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93presenter)? Or something else? – Peter Mortensen Aug 12 '22 at 23:25
1

Solution

Get all parents

var paths       = event['path'] as Array<any>;

Checks if any parent is the component

var inComponent = false;    
paths.forEach(path => {
    if (path.tagName != undefined) {
        var tagName = path.tagName.toString().toLowerCase();
        if (tagName == 'app-component')
            inComponent = true;
    }
});

If you have the component as parent then click inside the component

if (inComponent) {
    console.log('clicked inside');
}else{
    console.log('clicked outside');
}

Complete method

@HostListener('document:click', ['$event'])
clickout(event: PointerEvent) {
    
    var paths       = event['path'] as Array<any>;
    
    var inComponent = false;    
    paths.forEach(path => {
        if (path.tagName != undefined) {
            var tagName = path.tagName.toString().toLowerCase();
            if (tagName == 'app-component')
                inComponent = true;
        }
    });
    
    if (inComponent) {
        console.log('clicked inside');
    }else{
        console.log('clicked outside');
    }
    
}
Wictor Chaves
  • 957
  • 1
  • 13
  • 21
0

You can call an event function like (focusout) or (blur); then you would put in your code:

<div tabindex=0 (blur)="outsideClick()">raw data </div>

outsideClick() {
  alert('put your condition here');
}
TylerH
  • 20,799
  • 66
  • 75
  • 101
Rahul Yadav
  • 67
  • 1
  • 5
0

nice and tidy with rxjs. i used this for aggrid custom cell editor to detect clicks inside my custom cell editor.

private clickSubscription: Subscription | undefined;

public ngOnInit(): void {
this.clickSubscription = fromEvent(document, "click").subscribe(event => {
      console.log("event: ", event.target);
      if (!this.eRef.nativeElement.contains(event.target)) {
        // ... click outside
      } else {
        // ... click inside
    });
    
    
public ngOnDestroy(): void {
    console.log("ON DESTROY");
    this.clickSubscription?.unsubscribe();
  }
  
  
pinarella
  • 805
  • 8
  • 16