我有两个需要访问共享变量的控制器,这是通过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;
}
}
此服务组件在两个控制器之间共享值时效果很好,但是,当重新加载/刷新页面时,共享变量无法保留该值。我想知道是否应该使用浏览器中的local storage
来缓存变量值。