Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call callback function on esc in Angular UI Bootstrap's modal

I'm using a modal of Angular UI Bootstrap that can be closed via a Cancel button or via ESC. As I have to do some cleaning when it closes, I've written the 'cancel' method in the scope, but this is only called when clicking this Cancel button, how could I do to call this cleaning functions when it closes on ESC too?

like image 298
farolfo Avatar asked Feb 12 '14 20:02

farolfo


2 Answers

When a modal is dismissed (either by pressing ESC or clicking on backdrop) a promise returned from the $modal.open method call is rejected. So you can react on ESC press by adding error handler to the returned promise. This is illustrated in the example available from the demo page: http://plnkr.co/edit/xMTr78WJQbKyHsA53gyv?p=preview (see this line: $log.info('Modal dismissed at: ' + new Date());)

like image 69
pkozlowski.opensource Avatar answered Oct 07 '22 23:10

pkozlowski.opensource


thanks @pkozlowski.opensource

i directly knew what to do :)

var modalWindow = $modal.open({
    windowClass: 'modal myKewlDialog',
    templateUrl: 'views/modals/myKewlModalTemplate.html',
    controller: 'myKewlModalController'
});

modalWindow.result.then(function (result) {
    updateUI();
}, function (result) {
    updateUI();
});
like image 27
Guntram Avatar answered Oct 07 '22 23:10

Guntram