I am using AngularJS (1.5.9) in my app and ui-bootstrap. I would like to show a confirm popup in case the user clicks outside the already open modal.
I tried to use the controlled exit with $uibModalInstance.close()
and the non controlled exit with $uibModalInstance.dismiss()
but the modal closes afterwards and once I show my confirm popup it is already too late.
Tried to use backdrop: 'static'
attribute but couldn't catch a click event outside the modal window.
HTML:
<div ng-app="myApp">
<script type="text/ng-template" id="myModal.html">
<div class="modal-header">
<h3 class="modal-title">Modal title</h3>
</div>
<div class="modal-body">
Modal content
</div>
<div class="modal-footer">
<button class="btn btn-primary" ng-click="ok()">OK</button>
<button class="btn btn-warning" ng-click="cancel()">Cancel</button>
</div>
</script>
<div ng-controller="MyCtrl">
<input type="button" value="Show modal" ng-click="showModal()"/>
</div>
</div>
JS:
angular.module("myApp", ['ui.bootstrap'])
.controller("MyCtrl", function($scope, $modal) {
$scope.showModal = function(){
$modal.open({
templateUrl: 'myModal.html',
controller: 'ModalDialogController',
})
.result.then(
function () {
alert("OK");
},
function () {
alert("Cancel");
}
);
}
})
.controller("ModalDialogController", function ($scope, $modalInstance) {
$scope.ok = function () {
$modalInstance.close();
};
$scope.cancel = function () {
$modalInstance.dismiss('Cancel');
};
});
Here is a related fiddle --> Fiddle
As you can see, once opening the modal and clicking outside, there is an alert with 'cancel' but I would like to pop another modal to confirm this action to close the modal and in case the user clicks 'cancel' the previous modal stays open. in case the user clicks 'ok' close both modals.
Any ideas?