import { Directive , HostListener , Input , OnInit , OnDestroy } from '@angular/core' ; @ Directive ( { selector : '[offClick]' } ) export class OffClickDirective implements OnInit , OnDestroy { @ Input ( 'offClick' ) public offClickHandler : any ; @ HostListener ( 'click' , [ '$event' ] ) public onClick ( $event ) { $event . stopPropagation ( ) ; } public ngOnInit ( ) { setTimeout ( ( ) => { if ( typeof document !== 'undefined' ) { document . addEventListener ( 'click' , this . offClickHandler ) ; } } , 0 ) ; } public ngOnDestroy ( ) { if ( typeof document !== 'undefined' ) { document . removeEventListener ( 'click' , this . offClickHandler ) ; } } }