繁体   English   中英

如何使用Angular8关闭基于外部点击的模式弹出窗口

[英]How to close modal popup based on outside click using Angular8

演示

我通过在其他组件中调用它的选择器来调用一个组件,所以它显示为弹出窗口,但我想在我做外部点击时关闭它,任何人都可以帮助我如何做到这一点。

HTML 组件 1:

 <div *ngIf="editAdditionalComment">
        <app-comment></app-comment>
  </div>

评论组件:

<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">&times;</span>
                </button>
              </div>
            <div class="modal-body">
        <h6>Hello Hai</h6>
              
            </div>  
        </div>
    </div>
    </div>

这是我使用的指令:

    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

用法:

 <div inigoClickOutside (outsideClick)="onOutsideClick()"></div>

您可以创建一个识别点击的背景 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">&times;</span> </button> </div> <div class="modal-body"> <h6>Hello Hai</h6> </div> </div> </div> </div> <div>

如果需要将事件返回给父组件,可以创建一个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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM