javascript - Clicking on multiple divs but showing only one class (Angular2)? -
i'm working on modal window in angular , when open modal window , click outside modal window (shaded background) wrong class of div element.
example: click on modal , modal class appears ok, when click on shaded background modal class appears again, why? class should appear instead.
here code (typescript file):
@viewchild('ova') ova; //accessing div via refference tag in html @hostlistener('document:click') onclick($event){ if (this.show) console.log("display event: "+ this.ova.nativeelement.classname)
here corresponding .html file:
<div class="modalall"> <div *ngif="closeit" (click)="close()"n class="modalclose">x</div> <div #ova class="modal"> text </div> </div>
try (better in directive):
@hostlistener('document:click', ['$event', '$event.target']) onclick(event: mouseevent, targetelement: htmlelement): void { if (!targetelement) { return; } const clickedinside = this.elementref.nativeelement.contains(targetelement); if (!clickedinside) { this.clickoutside.emit(event); } }
Comments
Post a Comment