简体   繁体   English

如果用户在弹出窗口旁边单击,Angular4弹出窗口会关闭吗?

[英]Angular4 pop up is closing if user click out side the pop up?

I followed this tutorial for pop up modal in angular4: 我按照本教程在angular4中弹出模态:

http://jasonwatmore.com/post/2017/01/24/angular-2-custom-modal-window-dialog-box http://jasonwatmore.com/post/2017/01/24/angular-2-custom-modal-window-dialog-box

If I click outside of the modal, the modal disappears. 如果我单击模态之外,则模态消失。 But I do not want the modal to disappear until user clicks the close icon. 但是我不希望模式消失,直到用户单击关闭图标。

Here is the modal's OnInit : 这是模态的OnInit

ngOnInit(): void {
        let modal = this;

        // ensure id attribute exists
        if (!this.id) {
            console.error('modal must have an id');
            return;
        }

        // move element to bottom of page (just before </body>) so it can be displayed above everything else
        this.element.appendTo('body');

        // close modal on background click
        this.element.on('click', function (e: any) {
            var target = $(e.target);
            if (!target.closest('.modal-body').length) {
                modal.close();
            }
        });

        // add self (this modal instance) to the modal service so it's accessible from controllers
        this.modalService.add(this);
    }

Here is the plunker of what I have actually: https://plnkr.co/edit/gPCTvV?p=preview 这是我实际拥有的东西: https ://plnkr.co/edit/gPCTvV?p=preview

Just remove this from the app/_directives/modal.component.ts 只需将其从app/_directives/modal.component.ts

    // close modal on background click
    this.element.on('click', function (e: any) {
        var target = $(e.target);
        if (!target.closest('.modal-body').length) {
            modal.close();
        }
    });

Updated Plunker LINK 更新了Plunker LINK

Update 更新

You can make use of material dialogue or make use of bootstrap modal. 您可以使用实质性对话或使用引导模态。

For material dialogue live example check this working link and click on contact. 有关实时对话的示例,请检查此工作链接 ,然后单击“联系”。

Also you can check this angular materail link 您也可以检查此角度材料轨道

For your purpose where you don't want the modal to be closed when clicked outside check out this plunker . 为了您的目的,当您不希望在外部单击时关闭模态时,请检查此柱塞

to use bootstrap modal check this answer it has a working demo too 使用引导程序模态检查此答案它也有一个正常的演示

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

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