diff --git a/js/ext/angular/src/directive/ionicContent.js b/js/ext/angular/src/directive/ionicContent.js index a1e9ddc18a..ce20673607 100644 --- a/js/ext/angular/src/directive/ionicContent.js +++ b/js/ext/angular/src/directive/ionicContent.js @@ -97,8 +97,11 @@ angular.module('ionic.ui.content', ['ionic.ui.service', 'ionic.ui.scroll']) $scope.$on('$viewContentLoaded', function(e, viewHistoryData) { viewHistoryData || (viewHistoryData = {}); - if (viewHistoryData.scrollValues) { - scrollView.scrollTo(viewHistoryData.scrollValues); + var scroll = viewHistoryData.scrollValues; + if (scroll) { + $timeout(function() { + scrollView.scrollTo(+scroll.left || null, +scroll.top || null); + }, 0); } //Save scroll onto viewHistoryData when scope is destroyed diff --git a/js/ext/angular/test/directive/ionicContent.unit.js b/js/ext/angular/test/directive/ionicContent.unit.js index 08e23a19c9..abca653bf1 100644 --- a/js/ext/angular/test/directive/ionicContent.unit.js +++ b/js/ext/angular/test/directive/ionicContent.unit.js @@ -1,6 +1,6 @@ describe('Ionic Content directive', function() { var compile, element, scope; - + beforeEach(module('ionic.ui.content')); beforeEach(inject(function($compile, $rootScope, $timeout, $window) { @@ -10,7 +10,7 @@ describe('Ionic Content directive', function() { window = $window; ionic.Platform.setPlatform('Android'); })); - + it('Has content class', function() { element = compile('')(scope); expect(element.hasClass('scroll-content')).toBe(true); @@ -91,10 +91,20 @@ describe('Ionic Content directive', function() { compileWithParent(); spyOn(scope.scrollView, 'scrollTo'); var scrollValues = { top: 40, left: -20, zoom: 3 }; - scope.$broadcast('$viewContentLoaded', { + scope.$broadcast('$viewContentLoaded', { scrollValues: scrollValues }); - expect(scope.scrollView.scrollTo).toHaveBeenCalledWith(scrollValues); + timeout.flush(); + expect(scope.scrollView.scrollTo.mostRecentCall.args).toEqual([-20, 40]); + }); + + it('should set null with historyData.scrollValues not valid', function() { + compileWithParent(); + spyOn(scope.scrollView, 'scrollTo'); + var scrollValues = { left: 'bar', top: 'foo' }; + scope.$broadcast('$viewContentLoaded', { scrollValues: scrollValues }); + timeout.flush(); + expect(scope.scrollView.scrollTo.mostRecentCall.args).toEqual([null, null]); }); it('should save scroll on the historyData passed in on $destroy', function() {