angularjsangular-ui-bootstrapangular-bootstrap

Possibly unhandled rejection when try to dismiss a modal


I have a cancel button on my modal and has an on-click function that calls:

onCancelClick: function () {
    $uibModalInstance.dismiss()
}

its work but make this error:

Possibly unhandled rejection: undefined

or when click on esc key:

Possibly unhandled rejection: escape key press

I know I can use below code in my config and turn these type of errors off:

app.config(function ($qProvider) {
    $qProvider.errorOnUnhandledRejections(false);
});

but i want to solve it. Do you know how can I fix this?


Solution

  • To avoid a possibly unhandled rejection message, simply handle the rejection:

    var modalPromise = $uibModal.open(options).result;
    
    modalPromise
      .then(function(result) {
        console.log("Modal closed with result", result);
    }).catch(function(reason) {
        console.log("Modal dismissed with reason", reason);
    });
    

    The $uibModal.open method returns an object of which the result property is a promise that settles either as fulfilled with the result argument of the .close operation, or as rejected with the reason argument of the .dismiss operation.

    For more information, see UI-Bootstrap Directive API Reference - uib.bootstrap.modal