好的解决方案来了!这有点 hacky,但非常适合我的情况。
第 1 步
在打开对话框时将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);
});
}
第 2 步
编写常用的关闭按钮处理函数
// 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();
}
}
第 3 步
在控制器中写一个关闭函数来调用工厂函数
/**
* Close sample location modal
*/
$scope.closeForm = function($event){
PopUpFactory.closeForm($scope.formName.$dirty,$scope,$event);
}
第 4 步
在为 ngDialog 的 HTML 定义标题/标题后添加以下行
<div id="SOME_ID" class="ngdialog-close" ng-click="closeForm($event)"></div>
哟...完成了工作...!!!
这个解决方案最好的部分是关闭任何表单的通用代码,所以一旦你完成了工厂功能,你只需要在 HTML 中任何需要的地方添加关闭按钮,并在控制器中添加简单的关闭功能