Advertisement
Guest User

Untitled

a guest
Feb 24th, 2019
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. import { Directive, Output, EventEmitter } from '@angular/core';
  2.  
  3. @Directive({
  4. selector: '[clickOutside]',
  5. host: {
  6. "(click)": "trackEvent( $event )", // we can use instead => @HostListener('click', ['$event'])
  7. "(document: click)": "compareEvent( $event )" // we can use instead => @HostListener('document:click', ['$event'])
  8. }
  9. })
  10. export class ClickOutsideDirective {
  11. @Output('clickOutside') clickOutside:EventEmitter<any> = new EventEmitter();
  12.  
  13. private localEvent = null;
  14. constructor() {}
  15.  
  16. /** Compare event at the Document level to a reference of the Element:click
  17. * This method triggers when we are on Document level - Document was clicked or event bubbling
  18. * If the Document click DON'T MATCH the Event:click reference => than the click is from outside of the Element
  19. * @param event
  20. */
  21. compareEvent(event){
  22. if ( event !== this.localEvent ) {
  23. this.clickOutside.emit( event );
  24. }
  25. this.localEvent = null;
  26. }
  27.  
  28.  
  29. /** Track user click from inside the bound target
  30. * We use this to track the click Event when it bubbles up the DOM tree
  31. * @param event
  32. */
  33. trackEvent(event){
  34. this.localEvent = event;
  35. }
  36. }
  37.  
  38. ---- html----
  39. (clickOutside)="hideSnrDetail($event)"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement