2017-10-28 70 views
0

外駁回角2+引導警告我在角2 web應用程序下面的警告:通過單擊警報

HTML

<div class="alert alert-primary alert-dismissible fade show" role="alert" *ngIf="this.MiddleC == true"> 
    <strong>YES!!! That's Middle C!!!</strong> 
     <button type="button" class="close" data-dismiss="alert" aria-label="Close"> 
     <span aria-hidden="true">&times;</span> 
     </button> 
    </div> 

我如何寫關閉一個TS方法當我點擊DOM中的其他任何地方時這個警報?不知道如何去做這件事。我需要從所有其他元素在DOM調用這個函數或者是有辦法解決?

回答

2

一個解決這個問題將是創造更多的「疊加」元素的方法,這將基本上奠定剛剛在DOM的模式之下。 你的HTML應該是類似的地方:

<div class="overlay" (click)="this.MiddleC = false"></div> 
<div class="alert alert-primary alert-dismissible fade show" role="alert" *ngIf="this.MiddleC == true"> 
    <strong>YES!!! That's Middle C!!!</strong> 
    <button type="button" class="close" data-dismiss="alert" aria-label="Close"> 
    <span aria-hidden="true">&times;</span> 
    </button> 
</div> 

然後CSS:

.overlay{ 
    position: fixed; 
    top: 0; 
    left: 0; 
    width: 100%; 
    height: 100%; 
    z-index: MODAL_ZINDEX - 1; 
} 

其次方式來處理它,可能是@HostListener,這應該工作:

@HostListener('window:click', ['$event']) 
onWindowClick($event){ 
    //check if its not modal that is clicked 
} 

https://angular.io/api/core/HostListener