farolfo
farolfo

Reputation: 388

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?

Upvotes: 12

Views: 14765

Answers (2)

Guntram
Guntram

Reputation: 980

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();
});

Upvotes: 8

pkozlowski.opensource
pkozlowski.opensource

Reputation: 117370

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());)

Upvotes: 22

Related Questions