[英]How to close modal popup based on outside click using Angular8
i am calling a component by calling its selector in other component, so it appears as popup, but i want to close that when i do outside click, can anyone help me how can that be done.我通过在其他组件中调用它的选择器来调用一个组件,所以它显示为弹出窗口,但我想在我做外部点击时关闭它,任何人都可以帮助我如何做到这一点。
HTML Component1: HTML 组件 1:
<div *ngIf="editAdditionalComment">
<app-comment></app-comment>
</div>
Comment Component:评论组件:
<div id="additionalCommentModal" class="dialog-container" keytestdirective (goListingPage)="keyDetails($event)" >
<div class="modal-dialog modal-dialog-centered" data-backdrop="static" role="document" >
<div class="modal-content" >
<div class="modal-header bg-info">
<h5 class="modal-title text-white">{{editModeData.modeHeader}}</h5>
<button type="button" class="close font-lg" data-dismiss="modal" aria-label="Close" (click)="cancelChanges()">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<h6>Hello Hai</h6>
</div>
</div>
</div>
</div>
Here's a Directive that I use:这是我使用的指令:
import { Directive, ElementRef, Output, EventEmitter, HostListener } from '@angular/core';
@Directive({
selector: '[inigoClickOutside]'
})
export class ClickOutsideDirective {
//----------------------------------------------------------------------//
@Output("outsideClick")
public clickOutside = new EventEmitter<MouseEvent>();
//----------------------------------------------------------------------//
constructor(private _elementRef: ElementRef) {
}
//----------------------------------------------------------------------//
@HostListener('document:click', ['$event.path'])
public onGlobalClick(targetElementPath: Array<any>) {
const elementRefInPath = targetElementPath.find(e => e === this._elementRef.nativeElement);
if (!elementRefInPath) {
this.clickOutside.emit(null);
}
}
//----------------------------------------------------------------------//
}//Cls
Usage:用法:
<div inigoClickOutside (outsideClick)="onOutsideClick()"></div>
You can create a background div that recognize the click, so in your code will be like this:您可以创建一个识别点击的背景 div,因此在您的代码中将如下所示:
.div-background{ position: fixed; height: 100vh; width: 100vw; }
<div class="div-background" (click)="editAdditionalComment = false"> <div id="additionalCommentModal" class="dialog-container" keytestdirective (goListingPage)="keyDetails($event)" > <div class="modal-dialog modal-dialog-centered" data-backdrop="static" role="document" > <div class="modal-content" > <div class="modal-header bg-info"> <h5 class="modal-title text-white">{{editModeData.modeHeader}}</h5> <button type="button" class="close font-lg" data-dismiss="modal" aria-label="Close" (click)="cancelChanges()"> <span aria-hidden="true">×</span> </button> </div> <div class="modal-body"> <h6>Hello Hai</h6> </div> </div> </div> </div> <div>
If you need to return to the parent component the event, you can create a Output:如果需要将事件返回给父组件,可以创建一个Output:
@Output() onClickBackground = new EventEmitter() this.onClickBackground.emit(true)
<div *ngIf="isShowingComment"> <app-comment (onClickBackground)="isShowingComment = false"></app-comment> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.