1

I have two controllers that need to access a shared variable, which is done through a service component in AngularJS:

angular.module('app').service('cacheService', cacheService);

cacheService.$inject = ['$q'];

function cacheService($q) {
  this.sharedVariable = null;

  this.getValue = function() {
     return this.sharedVariable;
   }

  this.setValue = function(newVal) {
     this.sharedVariable = newVal;
   }
}

This service component works well in sharing the value between two controllers, however, when the page is reloaded/refreshed, the shared variable cannot keep the value. I am wondering if I should use the local storage in browsers to cache the variable value.

TonyW
  • 18,375
  • 42
  • 110
  • 183
  • sounds like a good idea .. here is an SO post which shows storage techniques: [link](http://stackoverflow.com/questions/18247130/how-to-store-the-data-to-local-storage) and here's an external post: [link](http://www.undefinednull.com/2014/02/25/angularjs-real-time-model-persistence-using-local-storage/) GL! – Shehryar Abbasi May 18 '15 at 00:35
  • here's an SO post [link](http://stackoverflow.com/questions/19304435/local-storage-vs-angularjs-cachefactory) which discusses `local storage` vs `$cacheFactory` and also shows plug-n-play modules like `angular-local-storage` which is here: [link](http://gregpike.net/demos/angular-local-storage/demo/demo.html) and `angular-cache` which is here: [link](http://jmdobry.github.io/angular-cache/) – Shehryar Abbasi May 18 '15 at 00:59

0 Answers0