How do I prevent angular-ui modal from closing?

后端 未结 6 792
再見小時候
再見小時候 2021-01-30 05:15

I am using Angular UI $modal in my project http://angular-ui.github.io/bootstrap/#/modal

I don\'t want user to close the modal by pressing on backdrop. I want a modal

6条回答
  •  深忆病人
    2021-01-30 05:34

    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.

提交回复
热议问题