diff --git a/js/angular/service/actionSheet.js b/js/angular/service/actionSheet.js index 6cd53c056f..9fa3414967 100644 --- a/js/angular/service/actionSheet.js +++ b/js/angular/service/actionSheet.js @@ -82,6 +82,8 @@ function($rootScope, $document, $compile, $animate, $timeout, $ionicTemplateLoad * the action sheet, or false to keep it opened. * - `{function=}` `destructiveButtonClicked` Called when the destructive button is clicked. * Return true to close the action sheet, or false to keep it opened. + * - `{boolean=}` `cancelOnStateChange` Whether to cancel the actionSheet when navigating + * to a new state. Default true. * * @returns {function} `hideSheet` A function which, when called, hides & cancels the action sheet. */ @@ -94,14 +96,20 @@ function($rootScope, $document, $compile, $animate, $timeout, $ionicTemplateLoad buttonClicked: angular.noop, $deregisterBackButton: angular.noop, buttons: [], + cancelOnStateChange: true }, opts || {}); + // Compile the template var element = scope.element = $compile('')(scope); // Grab the sheet element for animation var sheetEl = jqLite(element[0].querySelector('.action-sheet-wrapper')); + var stateChangeListenDone = scope.cancelOnStateChange ? + $rootScope.$on('$stateChangeSuccess', function() { scope.cancel(); }) : + angular.noop; + // removes the actionSheet from the screen scope.removeSheet = function(done) { if (scope.removed) return; @@ -110,6 +118,8 @@ function($rootScope, $document, $compile, $animate, $timeout, $ionicTemplateLoad sheetEl.removeClass('action-sheet-up'); $document[0].body.classList.remove('action-sheet-open'); scope.$deregisterBackButton(); + stateChangeListenDone(); + scope.cancel.$scope = null; //see last line $animate.removeClass(element, 'active', function() { scope.$destroy(); diff --git a/test/unit/angular/service/actionSheet.unit.js b/test/unit/angular/service/actionSheet.unit.js index 4d1dd0dc3e..e98b5424a1 100644 --- a/test/unit/angular/service/actionSheet.unit.js +++ b/test/unit/angular/service/actionSheet.unit.js @@ -84,4 +84,20 @@ describe('Ionic ActionSheet Service', function() { expect(cancelSpy).toHaveBeenCalled(); })); + it('should cancelOnStateChange by default', inject(function($rootScope) { + var scope = setup(); + spyOn(scope, 'cancel'); + $rootScope.$broadcast('$stateChangeSuccess'); + expect(scope.cancel).toHaveBeenCalled(); + })); + + it('should not cancelOnStateChange with option as false', inject(function($rootScope) { + var scope = setup({ + cancelOnStateChange: false + }); + spyOn(scope, 'cancel'); + $rootScope.$broadcast('$stateChangeSuccess'); + expect(scope.cancel).not.toHaveBeenCalled(); + })); + });