2

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?

georgeawg
  • 48,608
  • 13
  • 72
  • 95
Alireza
  • 23
  • 2
  • 6

3 Answers3

5

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

georgeawg
  • 48,608
  • 13
  • 72
  • 95
  • 1
    I do like this because I don't want to do anything after dismiss modal: `var modalInstance = $uibModal.open({ //options }); modalInstance.result.then(angular.noop, angular.noop); ` – Alireza Oct 14 '18 at 13:30
  • If this answers your question, mark it accepted so that others will know the question is answered. – georgeawg Oct 14 '18 at 20:26
0

Using $uibModalInstance.close(false) instead of $uibModalInstance.dismiss() will solve this issue

Maanas Hegde
  • 67
  • 2
  • 9
-1

If you don't want to pass any reason, you can use $uibModalInstance.close(). You can have catch block like:

try{    
  $uibModalInstance.dismiss();
} catch (err){
//Check what error you are getting.
}

Could be the error is just for reason parameter, which is expected in dismiss function, though I am not sure about that.

PM.
  • 1,735
  • 1
  • 30
  • 38