简体   繁体   English

通过背景检测关闭$ modal

[英]Detect closing $modal by backdrop

I'm using AngularJS with angularUI-bootstrap. 我正在使用AngularJS和angularUI-bootstrap。 Is there a way to detect when a modal is being closed by clicking on the backdrop? 有没有办法通过点击背景来检测何时关闭模态? I am trying to change a boolean based on closing of the modal. 我试图根据模态的关闭更改布尔值。

Sure, it is very easy to detect closing by ESC / clicking on backdrop. 当然,通过ESC /点击背景来检测关闭非常容易。 If such an event takes place the result promise will be rejected. 如果发生此类事件, result承诺将被拒绝。 So, you can run whatever logic you want by adding error handler to the result promise returned from the $modal.open({...}) method. 因此,您可以通过向$modal.open({...})方法返回的result保证添加错误处理程序来运行您想要的任何逻辑。

You can see this in action in a plunker forked from the demo page ( http://angular-ui.github.io/bootstrap/ ): http://plnkr.co/edit/QJbjkB7BUT5VFInVPyrF?p=preview where the $log.info('Modal dismissed at: ' + new Date()); 您可以在演示页面( http://angular-ui.github.io/bootstrap/ )中分叉的plunker中看到这个: http ://plnkr.co/edit/QJbjkB7BUT5VFInVPyrF?p = preview $log.info('Modal dismissed at: ' + new Date()); code is executed on modal's dismiss. 代码在modal的dismiss上执行。

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 my way of doing it, thought it might be helpful for others who stumble across your question. 这是我这样做的方式,认为对于偶然发现问题的其他人可能会有所帮助。

You can do that like so: 你可以这样做:

var instance = modal.open({ ... })

instance.result.then(function success(){
 //Do success things
},function fail(reason){
  if( ~reason.indexOf('backdrop') ){ 
    // Do things when modal closes on backdrop click
  }
});

This is another quick way to catch a rejected promise (ie modal being dismissed either intentionally or by clicking the background): 这是另一种捕捉被拒绝承诺的快捷方式(即模态被故意或通过点击背景被解雇):

$modal.open({ ... })
.result.catch(function(){
   // do something
});

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

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