diff --git a/js/angular/service/viewService.js b/js/angular/service/viewService.js index 4e62039a93..9570522768 100644 --- a/js/angular/service/viewService.js +++ b/js/angular/service/viewService.js @@ -550,21 +550,23 @@ function($rootScope, $state, $location, $window, $injector, $animate, $ionicNavV histories = $rootScope.$viewHistory.histories, currentView = $rootScope.$viewHistory.currentView; - for(var historyId in histories) { + if(histories) { + for(var historyId in histories) { + + if(histories[historyId].stack) { + histories[historyId].stack = []; + histories[historyId].cursor = -1; + } + + if(currentView && currentView.historyId === historyId) { + currentView.backViewId = null; + currentView.forwardViewId = null; + histories[historyId].stack.push(currentView); + } else if(histories[historyId].destroy) { + histories[historyId].destroy(); + } - if(histories[historyId].stack) { - histories[historyId].stack = []; - histories[historyId].cursor = -1; } - - if(currentView.historyId === historyId) { - currentView.backViewId = null; - currentView.forwardViewId = null; - histories[historyId].stack.push(currentView); - } else if(histories[historyId].destroy) { - histories[historyId].destroy(); - } - } for(var viewId in $rootScope.$viewHistory.views) { @@ -573,7 +575,9 @@ function($rootScope, $state, $location, $window, $injector, $animate, $ionicNavV } } - this.setNavViews(currentView.viewId); + if(currentView) { + this.setNavViews(currentView.viewId); + } } }; diff --git a/test/unit/angular/service/viewService.unit.js b/test/unit/angular/service/viewService.unit.js index 22c50c09fe..998f03ee4b 100644 --- a/test/unit/angular/service/viewService.unit.js +++ b/test/unit/angular/service/viewService.unit.js @@ -697,6 +697,10 @@ describe('Ionic View Service', function() { }); })); + it('should not error when clearing empty history', function() { + expect(viewService.clearHistory.bind(null)).not.toThrow(); + }); + it('should create a viewService view', inject(function($location) { var newView = viewService.createView(); expect(newView).toEqual(null);