0

I use AngularJS and ngView in my project.

ngView works perfectly with ngRoute, but when I come back to the previus link, ngView reload again data (with Ajax).

Is it possible to "keep in memory" previus data, so that should not always recharge all?

Simone Sessa
  • 795
  • 1
  • 12
  • 35

1 Answers1

0

You should store your data in localStorage (or cookieStorage if localStorage is not available), and manage your own cache policy. Then check in your controller if the data is Available and not expired before showing it up. I always use a service of my own that goes like:

(function () {

var __service = function () {

    var __set = function (key, item) { localStorage.setItem(key, JSON.stringify(item)); };
    var __get = function (key) { return (localStorage.getItem(key) ? JSON.parse(localStorage.getItem(key)) : null); };
    var __clear = function (key) { localStorage.removeItem(key); };
    var __clearAll = function () { localStorage.clear(); };

    return {
        Set: __set,
        Get: __get,
        Clear: __clear,
        ClearAll: __clearAll
    };

};
angular.module('myApp').service('StorageService', __service);

}());

Then in my controllers I put that service to work in order to cache information that I do not want to retrieve everytime I get there.

PD: I know i must specialize that service to check if localStorage is available.

Janx from Venezuela
  • 1,147
  • 1
  • 10
  • 12
  • Thanks. I should to do something like the facebook app notifications: when I click on a notification a go on a page and when I go back, I come back on notification tab at same point.. Maybe I can't explain well, is it possibile to do this: http://it.tinypic.com/r/711gsz/8 ? Your code works for this? – Simone Sessa Mar 01 '15 at 17:05
  • Sorry, i really really don't understand what you're trying to accomplish. Are you trying to save the ViewState of your page ? remember that angular works on a SPA (Single Page Application) philosophy, therefore handling back button actions is somewhat tricky. – Janx from Venezuela Mar 01 '15 at 18:41
  • I would that when the user comes back, he sees the previous page exactly as he had left – Simone Sessa Mar 01 '15 at 22:04