简体   繁体   English

如何防止Angular-UI引导程序模式关闭?

[英]How to prevent Angular-UI bootstrap modal closing?

I do have a $modalInstance . 我有一个$modalInstance I can receive close event notification with the help of the promises: 我可以在promises的帮助下收到关闭事件通知:

$modalInstance.result.finally(function() {
  // code here
});

But I don't know how to prevent closing if user closes the model by mistake. 但是我不知道如果用户错误地关闭了模型,如何防止关闭。 I want to ask user if one really wants to close the model and close it if he does. 我想问用户是否真的要关闭模型,如果确实要关闭它。 Still I don't want to enable backdrop: 'static' : 仍然我不想启用backdrop: 'static'

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

Thank you. 谢谢。

I did some more research, and I found another question similar to this one, this is the answer found on that question (feel free to +1 him, and not me) 我做了一些进一步的研究,然后发现了另一个与此问题类似的问题 ,这是在该问题上找到的答案(请随意为他+1,而不是我)

    $scope.$on('modal.closing', function(event, reason, closed) {
    var r = prompt("Are you sure you wanna close the modal? (Enter 'YES' to close)");

    if (r !== 'YES') {
        event.preventDefault();
    }
});

Put this inside the modal controller. 将其放在模式控制器中。

This is not exactly what you searched for, this will prompt you if you try to close the modal, by dismiss (clicking outside modal), cancel or ok button. 这与您搜索的内容不完全相同,如果您尝试通过关闭(单击外部模态),取消或确定按钮来关闭模态,则会提示您。 You can try and modify it to suit your needs. 您可以尝试对其进行修改以适合您的需求。

Update: Added simple if else check to see what was clicked, and if backdrop was clicked then prompt the user: 更新:添加了简单的if else检查以查看单击了什么,如果单击了背景,则提示用户:

    $scope.$on('modal.closing', function(event, reason, closed) {

    if (reason == 'ok' || reason == 'cancel'){
        console.log('closed');
    } else {
        // this is if 'reason' == 'backdrop click' 
        var r = prompt("Are you sure you wanna close the modal? (Enter 'YES' to close)");

        if (r !== 'YES') {
            event.preventDefault();
        }
    }
});

Do you think this solution is sufficient? 您是否认为此解决方案足够?

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

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