How to detect click anywhere on the page by Typescript? in AngularJS 2
            Asked
            
        
        
            Active
            
        
            Viewed 1.1k times
        
    5
            
            
        - 
                    On what page ? Every pages ? – YounesM Apr 05 '17 at 14:56
- 
                    See also http://stackoverflow.com/questions/35527456/angular2-window-resize-event/35527852#35527852 – Günter Zöchbauer Apr 05 '17 at 15:13
1 Answers
18
            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
    }
}
 
    
    
        adharris
        
- 3,591
- 1
- 21
- 18
- 
                    thanks for the answer! I got error on documentClick. Any idea why? – Nasim Mokhtar Apr 05 '17 at 15:01
- 
                    documentClick(event: MouseEvent) { this.show = false; this.changeDetectorRef.detectChanges(); } – Nasim Mokhtar Apr 05 '17 at 15:02
- 
                    
- 
                    
- 
                    That error doesn't make sense to me; are you trying to call that function somewhere else? – adharris Apr 05 '17 at 15:08
- 
                    I fixed it! now I need to use this method in another method. What should I pass as a parameter? this.documentClick('click?'); – Nasim Mokhtar Apr 05 '17 at 15:28
