admin管理员组文章数量:1400123
could you please tell me how to close pop screen when use click outside in angular 4 ?
In my demo application I have one
button on click of button I am showing pop up screen (which is working find).I want it should close when User click outside but not on pop up screen
in other words it should close when user click other than pop up screen .I try to make directive
which detect click outside or inside but it not work for me here is my code
import { Directive,ElementRef } from '@angular/core';
@Directive({
selector: '[appOutside]',
host: {
'(document:click)': 'onClick($event)',
}
})
export class OutsideDirective {
constructor(private elementRef: ElementRef) { }
onClick(event) {
if (!this.elementRef.nativeElement.contains(event.target)) // or some similar check
alert('clicked')
//doSomething();
}
}
.directive.ts
could you please tell me how to close pop screen when use click outside in angular 4 ?
In my demo application I have one
button on click of button I am showing pop up screen (which is working find).I want it should close when User click outside but not on pop up screen
in other words it should close when user click other than pop up screen .I try to make directive
which detect click outside or inside but it not work for me here is my code
import { Directive,ElementRef } from '@angular/core';
@Directive({
selector: '[appOutside]',
host: {
'(document:click)': 'onClick($event)',
}
})
export class OutsideDirective {
constructor(private elementRef: ElementRef) { }
onClick(event) {
if (!this.elementRef.nativeElement.contains(event.target)) // or some similar check
alert('clicked')
//doSomething();
}
}
https://stackblitz./edit/angular-1xhibr?file=src%2Fapp%2Foutside.directive.ts
Share Improve this question asked Jul 21, 2018 at 19:26 user944513user944513 12.8k52 gold badges185 silver badges348 bronze badges2 Answers
Reset to default 2Just add class to DOM nodes of popup & outside of popup (or on appOutside element). Then just checkif click event triggered on DOM part inside of popup or outside. Update your view code to:
<div class="hover_bkgr_fricc" *ngIf="ispopUpShow" appOutside (click)="ClickedOut($event)">
<span class="helper"></span>
<div class="inside-popup">
<div class="popupCloseButton" (click)="closePop()">X</div>
<p>Add any HTML content<br />inside the popup box!</p>
</div>
</div>
Where those methods in ponent class can be:
closePop() {
this.ispopUpShow = false;
}
ClickedOut(event) {
if(event.target.className === "hover_bkgr_fricc") {
this.ispopUpShow = false;
}
}
Updated Stackblitz Example
All you actually need to do is move the appOutside
directive onto your modal, since we want to know if the click is outside of the modal. If you leave it on the hover_bkgr_fricc
div, it will always be inside, since it's styled to take up the whole viewport
<div class="hover_bkgr_fricc" *ngIf="ispopUpShow" >
<span class="helper"></span>
<div appOutside>
<div class="popupCloseButton">X</div>
<p>Add any HTML content<br />inside the popup box!</p>
</div>
</div>
That change alone should work.
Here is a fork with the change I made
本文标签: javascripthow to close pop screen when use click outside in angular 4Stack Overflow
版权声明:本文标题:javascript - how to close pop screen when use click outside in angular 4? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744139560a2592555.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论