Dropdown closing by clicking anywhere else

This commit is contained in:
francesco 2021-12-14 17:43:03 +01:00
parent 9d14df3a3b
commit 8b0a745358

View File

@ -1,12 +1,12 @@
import { Directive, HostListener, HostBinding } from '@angular/core';
import {Directive, ElementRef, HostBinding, HostListener} from '@angular/core';
@Directive({
selector: '[appDropdown]'
})
export class DropdownDirective {
@HostBinding('class.open') isOpen = false;
@HostListener('click') toggleOpen() {
this.isOpen = !this.isOpen;
@HostListener('document:click', ['$event']) toggleOpen(event: Event) {
this.isOpen = this.elRef.nativeElement.contains(event.target) ? !this.isOpen : false;
}
constructor(private elRef: ElementRef) {}
}