简体   繁体   English

单击 X 按钮时关闭 Angular 模态

[英]Close Angular Modal when I click on the X button

I am learning Angular and I made a Modal in my project.我正在学习Angular ,我在我的项目中做了一个Modal。 If I click on the class that has the openDialog() function it opens the modal.如果我单击具有openDialog() function 的 class,它会打开模式。

I want to be able to close that modal as well when I click on the X .我希望在单击X时也能够关闭该模式。

  <a class="portfolio__item" target="_blank" (click)="openDialog()">
        <img src="assets/img/mywork-img/portfolio-05.jpg" alt="" class="portfolio__img">
    </a>

<dialog id="my-dialog-pubcrawl" class="my-dialog">
   
   <div class="np-row">
    <div class="np-title">This is a dialog window</div>

    <div class="np-close-btn" title="Close">X</div> 
</div>
    
</dialog>

.ts file: This opens the modal .ts 文件:这将打开模态

  openDialog() {
    let myDialogPubcrawl:any = <any>document.getElementById("my-dialog-pubcrawl");
    myDialogPubcrawl.showModal();
}

The Modal:模式:

在此处输入图像描述

How can I close the modal by clicking on the X in Angular?如何通过单击 Angular 中的 X 关闭模式?

One way of achieveing that is to store the instance of the dialog and call the close method:实现这一点的一种方法是存储对话框的实例并调用关闭方法:

<a class="portfolio__item" target="_blank" (click)="openDialog()">
        <img src="assets/img/mywork-img/portfolio-05.jpg" alt="" class="portfolio__img">
    </a>

<dialog id="my-dialog-pubcrawl" class="my-dialog">
   
   <div class="np-row">
    <div class="np-title">This is a dialog window</div>

    <div class="np-close-btn" title="Close" (click)="closeDialog()">X</div> 
</div>
    
</dialog>
myDialogPubcrawl:any;

openDialog() {
    this.myDialogPubcrawl = <any>document.getElementById("my-dialog-pubcrawl");
    this.myDialogPubcrawl.showModal();
}

closeDialog() {
    this.myDialogPubcrawl.close();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在单击“x”按钮时关闭面板并使用angularjs单击屏幕?(新到角度) - How do I close the panel when clicked on 'x' button and click on the screen using angularjs?(new to angular) 单击按钮关闭模式 - close modal on button click 当我单击切换按钮时(在重新打开的模态中)关闭并重新打开模态额外的单击事件触发器 - close and reopen modal extra click event trigger again when I do click on toggle button (in re-opened modal) 在按钮单击 Elementor 时关闭模态 - Close modal on button click Elementor 我想在单击添加按钮后关闭模式 - i want to close the modal after i click add button 当我们点击保存或取消按钮时如何关闭弹出模式 - How to close the popup modal when we click on save or cancel button React-Bootstrap 2022:单击 X 按钮时模态框不会关闭 - React-Bootstrap 2022: Modal Will Not Close When X button clicked 当我单击 X 或外部时,模态不会关闭 - Modal not closing when I click X or outside 如何在模态弹出窗口中添加 X 关闭按钮和外部单击关闭功能? - How to add X close button and outside click close function on modal popup? 单击按钮时渲染模态-React - Render a modal when I click a button - React
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM