How do I prevent angular-ui modal from closing?

AngularjsAngular UiAngularjs Service

Angularjs Problem Overview


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 can only be closed by pressing close button which I have created.

How do I prevent modal from closing ?

Angularjs Solutions


Solution 1 - Angularjs

While you creating your modal you can specify its behavior:

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

Solution 2 - Angularjs

backdrop : 'static'

Will work for 'click' events but still you can use "Esc" key to close the popup.

keyboard :false

to prevent popup close by "Esc" key.

Thanks to pkozlowski.opensource for answer.

I think question is duplicate of https://stackoverflow.com/questions/20371227/angular-ui-bootstrap-modal-how-to-prevent-user-interaction

Solution 3 - Angularjs

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.

Solution 4 - Angularjs

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.

static may work.

Solution 5 - Angularjs

Configure globally,

decorator, one of the best features in angular. gives the ability to "patch" 3rd party modules.

Let's say you want this behavior in all of your $modal references and you don't want to change your calls,

You can write a decorator. that simply adds to options - {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

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

Solution 6 - Angularjs

for the new version of ngDialog (0.5.6), use:

closeByEscape : false
closeByDocument : false

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRahul PrasadView Question on Stackoverflow
Solution 1 - Angularjsartur grzesiakView Answer on Stackoverflow
Solution 2 - AngularjsSachin G.View Answer on Stackoverflow
Solution 3 - AngularjsTiagoView Answer on Stackoverflow
Solution 4 - AngularjsChandermaniView Answer on Stackoverflow
Solution 5 - AngularjsJossef Harush KadouriView Answer on Stackoverflow
Solution 6 - AngularjsBen CohenView Answer on Stackoverflow