I have a long list of items that are links to the next page. I scroll down halfway, and tap an item.
When I press back (I am using the nav-router) the position of the list is set to the top. But I’d like to press back and be taken back to the previous page and previous scroll position.
I think this is because pressing back essentially re-loads the previous page.
Is there an Ionic solution to this problem? Or will I have to roll my own?
Did this get in the Ionic code that is now up? Do we have to do anything or will it just work this way (as this is something I really want too)
Thanks!
I don’t know if it will help but I threw this together and it feels close (but no cigar).
Basically this directive will capture the state name and position of scroll when you leave the page. It will then store the information in an object in an array in a service.
Then when the view is loaded again (on back for instance) the object in the array in the service is retrieved based on state name and the scroll position is applied.
.directive('keepScroll', function ($filter, $timeout, $state, $ionicScrollDelegate, $scrollPosition) {
return function ($scope, $element, $attrs) {
var scrollableElement = $element[0].querySelector('.scroll');
var view = $filter('filter')($scrollPosition.views, { name: $state.current.name })[0];
// Save scroll position on view change
$scope.$on("$stateChangeStart", function () {
// +++ how do I get this value? ++++
var position = 0; //
// Save existing view
if (view) view.position = position;
else {
// Add view to collection
$scrollPosition.views.push({
name: $state.current.name,
position: position
});
}
});
// Apply scroll position to view
$timeout(function () {
// +++ this doesn't exist but it would be nice if it did! +++
if (view) $ionicScrollDelegate.scrollTo(0, view.position, false);
}, 0);
}
})
It’s obviously not complete, but I just thought it might be useful if anyone were to tackle this problem.
Following this with interest… @gregorypratt, does this solution work for you? What are the drawbacks?
Also @max and @adam, will this be supported soon?
One little thought (that I am sure I can code around, only tested this 5 mins ago)
LIST > Detail
Scroll down detail page a little (as if you are reading something).
Back button to List - NOW at the vertical position you were before you went to detail - Great!
Go into another detail … now you are at vertical position you left 2 in… even though its a NEW detail page so I would prefer to be reading from the top of the new detail page…
So I guess on detail we need to not be scrolling down - or we need a simple scroll to top whenever you go to detail (which I am sure is possible, just not dug out how to do that yet)
Probably more of a documentation thing - make it clear in the list documents how to scroll to top - as I think this could be a cause of support questions otherwise