I have a function inside my controller that is called from a third party library requiring a boolean response. The function opens a modal (which is defined as a service) to get the response from the user and returns this as a promise. However, when dismiss is pressed on the modal I get an error in the console
Possibly unhandled rejection: cancel
I have spent hours trying to fix this with various methods but cannot remove the error whilst returning the true/false. Can anyone help?
Service
angular.module('app')
.service("ModalService", function ($uibModal) {
var modalDefaults = {
backdrop: true,
keyboard: true,
modalFade: true,
templateUrl: '/app/components/shared/modal.html'
};
var modalOptions = {
closeButtonText: 'No',
actionButtonText: 'Yes',
headerText: 'Title',
bodyText: 'Are you sure you wish to do this?'
};
this.showModal = function (customModalDefaults, customModalOptions) {
if (!customModalDefaults) {
customModalDefaults = {};
}
customModalDefaults.backdrop = 'static';
return this.show(customModalDefaults, customModalOptions);
};
this.show = function (customModalDefaults, customModalOptions) {
// Create temp objects to work with since we're in a singleton service
var tempModalDefaults = {};
var tempModalOptions = {};
// Map angular-ui modal custom defaults to modal defaults defined in service
angular.extend(tempModalDefaults, modalDefaults, customModalDefaults);
// Map modal.html $scope custom properties to defaults defined in service
angular.extend(tempModalOptions, modalOptions, customModalOptions);
if (!tempModalDefaults.controller) {
tempModalDefaults.controller = function ($scope, $uibModalInstance) {
$scope.modalOptions = tempModalOptions;
$scope.modalOptions.ok = function (result) {
$uibModalInstance.close(true);
};
$scope.modalOptions.close = function (result) {
$uibModalInstance.dismiss('cancel');
};
}
}
return $uibModal.open(tempModalDefaults).result;
};
});
Controller Function
this.onRemovingLabel = function($tag) {
var modalOptions = {
closeButtonText: 'Cancel',
actionButtonText: 'Delete',
headerText: 'Delete Label: ' + $tag.name,
bodyText: 'By removing this label all marked tags in the the dataset will be deleted. Do you wish to continue?'
};
return ModalService.showModal({}, modalOptions);
}
Function is called from HTML
<tags-input ... on-tag-removing="tc.onRemovingLabel($tag)">