简体   繁体   English

如何自定义ngDialog的关闭对话框功能?

[英]How to customize close dialog function for ngDialog?

I have to implement a customized close dialog function for close button of ngDialog . 我必须为ngDialog关闭按钮实现自定义的关闭对话框功能

As per requirement in some cases (where there is a form) I have to show another ngDialog confirm popup asking if user really want to close the dialog or not so there are 2 options 'YES' and 'NO' which has this behavior. 根据要求,在某些情况下(有表单),我必须显示另一个ngDialog确认弹出窗口,询问用户是否真的要关闭对话框,因此有两个选项“ YES”和“ NO”具有此行为。

I have tried it with preCloseCallback() method but somehow it did not worked for me as it does not wait for user confirmation . 我已经尝试过使用preCloseCallback()方法,但是由于它不等待用户确认 ,因此以某种方式对我不起作用 It is just like the function called on click of close and dialog closed or stays open depending on what I return from function immediately. 就像单击关闭,对话框关闭或保持打开状态时所调用的函数一样,这取决于我立即从函数返回的内容。 If I don't return anything it assumes it to be true and closes the dialog. 如果我不返回任何内容,则假定它为true,然后关闭对话框。

Can anybody please let me know the way to solve this issue? 有人可以让我知道解决此问题的方法吗?

Here comes the nice solutions! 这里有不错的解决方案! It's bit hacky but worked perfectly for my case. 有点古怪,但对于我的情况来说效果很好。

Step 1 第1步

Set showClose option false while opening dialog. 打开对话框时将showClose选项设置showClose false。

// In controller
PopUpFactory.openModal('SOME_NAME','SOME_URL.html', 'ngdialog-theme-default SOME_EXTRA_CSS', $scope, function(value){
    console.log("Done:", value);
},'SOME_CONTROLLER',false); // false passes to set **showClose** option false

// In common Factory
function openModal(name, templateUrl, classes, scope, callback, ctrl, showClose){
    if(showClose === undefined){
        showClose = true;
    }
    ngDialog.openConfirm({
        name: name,
        controller: ctrl,
        template: templateUrl,
        className: classes,
        closeByDocument: false, // to prevent popup close by clicking outside
        closeByEscape: false,   // to prevent popup close by ESC key 
        closeByNavigation : true, // to close popup on state navigation
        scope: scope,
        disableAnimation: true,
        showClose: showClose,
        preCloseCallback: function(value) {
            return true;
        }
    }).then(function (value) {
        callback(value);
    });
}

Step 2 第2步

Write common close button handling function 编写常用的关闭按钮处理功能

// In Common Factory
/**
 * Customize close for any open modal form
 * @param isDirty - flag saying if form is dirty
 * @param $scope - scope object of current open form
 * @param $event - $event object passed from close button of open form
 */
var closeConfirmOpen = false;
function closeForm(isDirty,$scope,$event){
    // following lines are important to prevent default behavior of ngDialog close event
    if($event){
        $event.preventDefault();
        $event.stopPropagation();   
    }

    if(isDirty){
        var msg = $filter('translate')('navigateAwayWithoutSavingConfirmMsg');
        closeConfirmOpen = true;
        confirmPopUp('Warning', msg, null, 'leavePage', 'red', 'stayOnPage', function(isOK){
            if(isOK == 1){ 
                $scope.closeThisDialog();
            }
            closeConfirmOpen = false;
        });
    }else{
        $scope.closeThisDialog();
    }
}

Step 3 第三步

Write a close function in controller to call factory function 在控制器中编写关闭函数以调用工厂函数

/**
 * Close sample location modal
 */
 $scope.closeForm = function($event){
     PopUpFactory.closeForm($scope.formName.$dirty,$scope,$event);
 }

Step 4 第四步

Add following line after defining header/title for HTML of ngDialog 在为ngDialog的HTML定义标题/标题后添加以下行

<div id="SOME_ID" class="ngdialog-close" ng-click="closeForm($event)"></div>

Yooo... done the job...!!! 哟...完成了工作... !!!

The best part of this solutions is a common code for closing any form, so once you done with factory function, you only need to add close button wherever required in HTML and add simple close function in controller 该解决方案的最好部分是用于关闭任何表单的通用代码,因此,完成工厂功能后,只需在HTML中需要的地方添加关闭按钮,并在控制器中添加简单的关闭功能

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

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