Disable View Refresh

Hi I understand that by default in Ionic, the view refreshes as soon as you navigate into it. Is there a simple way to disable this feature.

What do you mean by “refresh”? The controller get’s re-initialized?

@dave Absolutely Yes. How can I Stop this

This is a default of angular, which is what ionic is built on top of.

This is how angular handles things. Once a scope is no longer need by a view, it is destroyed ( to free up memory)

What are you trying to do?

@mhartington I have a list of items on one page. Basically when one item is clicked, a detail page for that item is navigated to. But when I navigate back, the old page reloads meaning the user has to scroll down to find that item again. Now I would like a situation where user gets back to where they where exactly on page.

You should check out our starter projects

What you need is to create an angular service so that way the data is present at all times during the application.

@mhartington I know how to use services but do they guarantee that the view and controller will not be reinitialised?

This goes against angular/ui-router. The view and controller will be destroyed once you navigate away from them. But injecting the data as a service to both controllers will at least keep the data loaded.

Look at this example.

1 Like

@mhartington how is possible that in your example the angular keeps the scroll position when you return back from pet detail. As you said: “The view and controller will be destroyed once you navigate away from them”. Which means when you navigate back everything should be destroyed except the data in service as it’s a singleton but that has nothing to do with the scroll position of the view (which was destroyed)

Ionic version 1.0.0-beta.14 introduced view caching. You can read up on caching on the official blog post: http://blog.ionic.io/the-final-beta/ or the ionNavView docs: http://ionicframework.com/docs/api/directive/ionNavView/

By default, views are cached to improve performance. When a view is navigated away from, its element is left in the DOM, and its scope is disconnected from the $watch cycle. When navigating to a view that is already cached, its scope is then reconnected, and the existing element that was left in the DOM becomes the active view. This also allows for the scroll position of previous views to be maintained.

1 Like

Oh I didn’t know there was such a important update! Thank you it’s exactly what I, and I guess a loooot of people needed. Was testing for last two days. Hopefully it will work well! Thank you again @brandyshea

You’re welcome. :slight_smile: You may want to check out the changelog, a lot changed with beta 14.

@brandyshea && @mhartington I just came back to this topic to say thank you for an amazing work! Best decision ever to cache the templates on change. Feel free to have a look at my implementation of angularJs and ionic http://m.tiltbook.com login: knplabs password: knplabs

Feel free to give me the feedback if you see something very bad in a structure that is against ionic “philosophy” :wink:

1 Like