Dropdown closing by clicking anywhere else
This commit is contained in:
parent
9d14df3a3b
commit
8b0a745358
@ -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) {}
|
||||
}
|
Loading…
Reference in New Issue
Block a user