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.