How to detect click anywhere on the page by Typescript? in AngularJS 2
1 Answer
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
}
}
6 Comments
Nasim Mokhtar
thanks for the answer! I got error on documentClick. Any idea why?
Nasim Mokhtar
documentClick(event: MouseEvent) { this.show = false; this.changeDetectorRef.detectChanges(); }
adharris
What is the error you're getting?
Nasim Mokhtar
Cannot find name 'documentClick'
adharris
That error doesn't make sense to me; are you trying to call that function somewhere else?
|