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({
|
@Directive({
|
||||||
selector: '[appDropdown]'
|
selector: '[appDropdown]'
|
||||||
})
|
})
|
||||||
export class DropdownDirective {
|
export class DropdownDirective {
|
||||||
@HostBinding('class.open') isOpen = false;
|
@HostBinding('class.open') isOpen = false;
|
||||||
|
@HostListener('document:click', ['$event']) toggleOpen(event: Event) {
|
||||||
@HostListener('click') toggleOpen() {
|
this.isOpen = this.elRef.nativeElement.contains(event.target) ? !this.isOpen : false;
|
||||||
this.isOpen = !this.isOpen;
|
|
||||||
}
|
}
|
||||||
|
constructor(private elRef: ElementRef) {}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user