diff --git a/src/loading-bar.js b/src/loading-bar.js index 44e092a..ada8f81 100644 --- a/src/loading-bar.js +++ b/src/loading-bar.js @@ -171,6 +171,7 @@ angular.module('cfp.loadingBar', []) this.$get = ['$injector', '$document', '$timeout', '$rootScope', function ($injector, $document, $timeout, $rootScope) { var $animate; + var $q; var $parentSelector = this.parentSelector, loadingBarContainer = angular.element(this.loadingBarTemplate), loadingBar = loadingBarContainer.find('div').eq(0), @@ -193,6 +194,9 @@ angular.module('cfp.loadingBar', []) if (!$animate) { $animate = $injector.get('$animate'); } + if (!$q) { + $q = $injector.get('$q'); + } $timeout.cancel(completeTimeout); @@ -294,24 +298,30 @@ angular.module('cfp.loadingBar', []) function _completeAnimation() { status = 0; started = false; + $rootScope.$broadcast('cfpLoadingBar:completed'); } function _complete() { if (!$animate) { $animate = $injector.get('$animate'); } + if (!$q) { + $q = $injector.get('$q'); + } _set(1); $timeout.cancel(completeTimeout); // Attempt to aggregate any start/complete calls within 500ms: completeTimeout = $timeout(function() { - var promise = $animate.leave(loadingBarContainer, _completeAnimation); - if (promise && promise.then) { - promise.then(_completeAnimation); - } - $animate.leave(spinner); - $rootScope.$broadcast('cfpLoadingBar:completed'); + var promiseBar = $animate.leave(loadingBarContainer); + var promiseSpinner = $animate.leave(spinner); + + $q.all([promiseBar, promiseSpinner]) + .then(function() { + _completeAnimation(); + }); + }, 500); }