Search code examples
angularjsbootstrap-modalangular-ui

backdrop: 'static' is not working for ngDialog modal


I want to prevent the modal from closing when clicked outside or pressing esc in keyboard. So I used backdrop:'static' and keyboard:false like below,

    var app = angular.module('MyApp', ['ngDialog', 'chatSocket']);

    app.controller('MainCtrl', function ($scope, ngDialog) {
    $scope.openChatBox = function() {
      ngDialog.openConfirm({
        template: 'chatBox.html',
        controller: 'msgController',
        backdrop: 'static',
        keyboard: false,
        scope: $scope //Pass the scope object if you need to access in the template
    }).then(
        function(value) {
            //You need to implement the saveForm() method which should return a promise object
            $scope.closeChat().then(

            );
        },
        function(value) {
            //Cancel or do nothing
        }
    );
};

});

The button clicked to open the modal is,

 <button ng-click="openChatBox()" >Open</button>

What is the problem with my code, why is not working?


Solution

  • For $modal, we use backdrop and keyboard options to achieve it, but for ngDialog the options are closeByDocument and closeByEscape.

    $scope.openChatBox = function() {
        ngDialog.openConfirm({
            template: 'chatBox.html',
            controller: 'msgController',
            closeByDocument: false,
            closeByEscape: false,
            scope: $scope //Pass the scope object if you need to access in the template
        }).then(
            function(value) {
                //You need to implement the saveForm() method which should return a promise object
                $scope.closeChat().then(
    
                );
            },
            function(value) {
                //Cancel or do nothing
            }
        );
    };