user3452075
user3452075

Reputation: 461

AngularJS mantain state when changing views?

Is there a way to maintain $scope when changing views and coming back?

When i load a certain view there is some heavy loading (loading data), but if i switch views and comeback to this one al the data is reloading again. Is there a way to prevent this?

I am not looking to share data between views, I just wish to have the old state when I return to the same view.

Upvotes: 1

Views: 54

Answers (1)

André Snede
André Snede

Reputation: 10045

There's a few ways to handle this.

$rootScope
You might be able to store the values in the $rootScope, I don't believe it changes when you use the internal routing module, but I would not recommend this.

Services (My Immediate recommendation)
You can use a static service, where the data can be stored, either constantly, or while changing route. Then you can load the data into the $scope again.

Caching
AngularJS has an inbuilt cache, you can use. This is great if you only need to store some of it, or for a smaller amount of time. Read the documentation for the CacheFactory here.

WebSQL or localstorage
For large amounts of data, you want to store between sessions, you can use the WebSQL database in most browsers, or store it as text in the localStorage in the browser.

Upvotes: 1

Related Questions