5

How to detect click anywhere on the page by Typescript? in AngularJS 2

2

1 Answer 1

20

You can scope a HostListener to the document.

import { Component, HostListener } from '@angular/core'    

class MyComponent {

    @HostListener('document:click', ['$event'])
    documentClick(event: MouseEvent) {
        // your click logic
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

thanks for the answer! I got error on documentClick. Any idea why?
documentClick(event: MouseEvent) { this.show = false; this.changeDetectorRef.detectChanges(); }
What is the error you're getting?
Cannot find name 'documentClick'
That error doesn't make sense to me; are you trying to call that function somewhere else?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.