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.

2
  • sounds like a good idea .. here is an SO post which shows storage techniques: link and here's an external post: link GL! Commented May 18, 2015 at 0:35
  • here's an SO post link which discusses local storage vs $cacheFactory and also shows plug-n-play modules like angular-local-storage which is here: link and angular-cache which is here: link Commented May 18, 2015 at 0:59

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.