简体   繁体   English

如何防止 angular-ui 模式关闭?

[英]How do I prevent angular-ui modal from closing?

I am using Angular UI $modal in my project http://angular-ui.github.io/bootstrap/#/modal我在我的项目中使用 Angular UI $modal http://angular-ui.github.io/bootstrap/#/modal

I don't want user to close the modal by pressing on backdrop.我不希望用户通过按下背景来关闭模式。 I want a modal can only be closed by pressing close button which I have created.我想要一个模式只能通过按下我创建的关闭按钮来关闭。

How do I prevent modal from closing ?如何防止模态关闭?

While you creating your modal you can specify its behavior:在创建模态时,您可以指定其行为:

$modal.open({
   // ... other options
   backdrop  : 'static',
   keyboard  : false
});
backdrop : 'static'

Will work for 'click' events but still you can use "Esc" key to close the popup.将适用于“点击”事件,但您仍然可以使用“Esc”键关闭弹出窗口。

keyboard :false

to prevent popup close by "Esc" key.以防止通过“Esc”键关闭弹出窗口。

Thanks to pkozlowski.opensource for answer.感谢 pkozlowski.opensource 的回答。

I think question is duplicate of Angular UI Bootstrap Modal - how to prevent user interaction我认为问题是Angular UI Bootstrap Modal 的重复——如何防止用户交互

Old question, but if you want to add confirmation dialogs on various close actions, add this to your modal instance controller:老问题,但如果您想在各种关闭操作上添加确认对话框,请将其添加到您的模态实例控制器中:

$scope.$on('modal.closing', function(event, reason, closed) {
    console.log('modal.closing: ' + (closed ? 'close' : 'dismiss') + '(' + reason + ')');
    var message = "You are about to leave the edit view. Uncaught reason. Are you sure?";
    switch (reason){
        // clicked outside
        case "backdrop click":
            message = "Any changes will be lost, are you sure?";
            break;

        // cancel button
        case "cancel":
            message = "Any changes will be lost, are you sure?";
            break;

        // escape key
        case "escape key press":
            message = "Any changes will be lost, are you sure?";
            break;
    }
    if (!confirm(message)) {
        event.preventDefault();
    }
});

I have a close button on the top right of mine, which triggers the "cancel" action.我的右上角有一个关闭按钮,它会触发“取消”操作。 Clicking on the backdrop (if enabled), triggers the cancel action.单击背景(如果启用),会触发取消操作。 You can use that to use different messages for various close events.您可以使用它为各种关闭事件使用不同的消息。

This is what is mentioned in documentation这是文档中提到的

backdrop - controls presence of a backdrop.背景 - 控制背景的存在。 Allowed values: true (default), false (no backdrop), 'static' - backdrop is present but modal window is not closed when clicking outside of the modal window.允许值:true(默认)、false(无背景)、'static' - 背景存在但在模态窗口外单击时不关闭模态窗口。

static may work. static可能有效。

Configure globally,全局配置,

decorator , one of the best features in angular. 装饰器角度中最好的功能之一。 gives the ability to "patch" 3rd party modules.提供“修补”第 3 方模块的能力。

Let's say you want this behavior in all of your $modal references and you don't want to change your calls,假设您希望在所有$modal引用中使用此行为并且不想更改调用,

You can write a decorator .你可以写一个装饰器 that simply adds to options - {backdrop:'static', keyboard:false}这只是添加到选项 - {backdrop:'static', keyboard:false}

angular.module('ui.bootstrap').config(function ($provide) {
    $provide.decorator('$modal', function ($delegate) {
        var open = $delegate.open;

        $delegate.open = function (options) {
            options = angular.extend(options || {}, {
                backdrop: 'static',
                keyboard: false
            });

            return open(options);
        };
        return $delegate;
    })
});
  • Note: in the latest versions, the $modal renamed to $uibModal注意:在最新版本中, $modal更名为$uibModal

Online demo - http://plnkr.co/edit/2MWIpOs3uAG5EFQy6Ndn?p=preview在线演示 - http://plnkr.co/edit/2MWIpOs3uAG5EFQy6Ndn?p=preview

for the new version of ngDialog (0.5.6), use:对于 ngDialog (0.5.6) 的新版本,请使用:

closeByEscape : false
closeByDocument : false

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

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