diff --git a/src/modal/modal.js b/src/modal/modal.js index c6215f4a11..1f49c04fa1 100644 --- a/src/modal/modal.js +++ b/src/modal/modal.js @@ -324,8 +324,11 @@ angular.module('ui.bootstrap.modal', []) $modalStack.close = function (modalInstance, result) { var modalWindow = openedWindows.get(modalInstance); if (modalWindow && broadcastClosing(modalWindow, result, true)) { - modalWindow.value.deferred.resolve(result); + // Defer resolution until after modal window is closed - #3787 removeModalWindow(modalInstance, modalWindow.value.modalOpener); + $timeout(function() { + modalWindow.value.deferred.resolve(result); + }); return true; } return !modalWindow; @@ -334,8 +337,11 @@ angular.module('ui.bootstrap.modal', []) $modalStack.dismiss = function (modalInstance, reason) { var modalWindow = openedWindows.get(modalInstance); if (modalWindow && broadcastClosing(modalWindow, reason, false)) { - modalWindow.value.deferred.reject(reason); + // Defer rejection until after modal window is dismissed - #3787 removeModalWindow(modalInstance, modalWindow.value.modalOpener); + $timeout(function() { + modalWindow.value.deferred.reject(reason); + }); return true; } return !modalWindow; diff --git a/src/modal/test/modal.spec.js b/src/modal/test/modal.spec.js index 0afe94ab8d..2c0c92e139 100644 --- a/src/modal/test/modal.spec.js +++ b/src/modal/test/modal.spec.js @@ -673,6 +673,40 @@ describe('$modal', function () { expect($document.find('.modal-backdrop')).not.toHaveClass('fade'); }); + it('should resolve the promise after the animation starts', function () { + var modal = open({ + template: '
Small modal dialog
', + animation: false + }); + + expect($document).toHaveModalsOpen(1); + + modal.result.then(function() { + expect($document).toHaveModalsOpen(0); + }, function() { + expect(true).toBe(false); + }); + + close(modal); + }); + + + it('should reject the promise after the animation starts', function () { + var modal = open({ + template: '
Small modal dialog
', + animation: false + }); + + expect($document).toHaveModalsOpen(1); + + modal.result.then(function() { + expect(true).toBe(false); + }, function() { + expect($document).toHaveModalsOpen(0); + }); + + dismiss(modal); + }); }); });