Preserve controller state when transitioning to another view

Hey guys, just wanted to see if it was possible to preserve a controller’s state when I transition to another view.

An example of this would be when I’m on the “feed” screen of my application. A user then clicks the settings and the view template changes. Once the user closes the settings screen, the feed screen appears again, but the data refreshes and http requests are made. Is it possible for the feed screen to NOT refresh when the settings screen closes?

Thanks!

You can use local storage to store the the feeds and provide a pull-to-refresh option (ion-resher) which will make a http request to get new feeds. This is the most common mechanism in mobile apps.
Instead of getting feeds every time from server you can get from local storage.

Alternatively you can cache the server requests with angular cache ($cacheFactory). This will still make a request but response will be very fast.

1 Like

No need to use cacheFactory if you can avoid to send the request another time (when nothing has changed).

Therefore you can use services.
Services are state independend -> make your request in a service and store the result in a variable in the service.
In your controller you use your service to send the request and access the result variable ;).

You can search the forum there are many posts about this.

https://docs.angularjs.org/guide/services

1 Like

Thanks for the help/heads up guys!

Used a combination of both solutions: implemented a pull-to-refresh that updates the service with new data, but the service acts as a cache/storage for the data. Works perfectly :smile: