Answer the question
In order to leave comments, you need to log in
How to close modal on click outside modal?
There is a modal and I need to make it so that when I click outside the modal, it closes. How to implement it?
Answer the question
In order to leave comments, you need to log in
@Directive({
selector: '[clickOutside]'
})
export class ClickOutsideDirective {
@Output('clickOutside') clickOutside = new EventEmitter();
constructor(
private elRef: ElementRef<HTMLElement>
) { }
@HostListener('document:click', ['$event.target']) onMouseClick(targetElement) {
const clickedInside = this.elRef.nativeElement.contains(targetElement);
if (!clickedInside) {
this.clickOutside.emit(targetElement);
}
}
}
<app-modalka (clickOutside)="onClickOutside($event)"></app-modalka>
you make a transparent fixed background on the whole screen, well, or not transparent, usually it is shaded a little, and you track the click on this background
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question