我的应用程序有问题。我有一个工厂,它调用prepareStorage
将我的所有静态变量保存restApi
到浏览器存储(使用ngStorage
)。看起来像这样:
angular.module('panelApp')
.factory('prepareStorage', ['$localStorage', '$sessionStorage', 'apiService',
function($localStorage, $sessionStorage, apiService) {
var promise = {};
var load = function() {
apiService.call('head', 'staticTypes.json', [], function(response, status, head) {
if(angular.isUndefined($localStorage.staticTypes) || $localStorage.staticTypes.updatedAt < new Date(head()['last-modified'])){
delete $localStorage.staticTypes;
promise = apiService.call('get', 'statictypes.json', [], function (response) {
$localStorage.staticTypes = response;
}, function (errorObj) {
console.log(errorObj.error.message);
});
}
});
return promise;
return {
load: load()
};
}]);
然后我宣布了第二个工厂constantsService
,它给了我存储变量:
angular.module('panelApp')
.factory('constantsService', ['$localStorage', '$q', 'prepareStorage', '$timeout',
function($localStorage, $q, prepareStorage, $timeout) {
var output = {};
var deferred = $q.defer();
$timeout(function() {
deferred.resolve(prepareStorage.load, function() {
output = {
status: $localStorage.staticTypes.status
};
});
}, 1000);
return output;
}]);
在这种情况下,我的麻烦来了。
如果不重新刷新,我无法在控制器中获取此存储变量。这意味着,在第一次刷新站点后,控制器找不到
$localStorage.staticTypes.status
对象,只有在我第二次刷新时才会找到。
我正在寻找一种避免$timeout
使用服务的干净解决方案。
提前致谢。