我需要一個解決方案以角度方式更改和訪問基於會話的存儲中的值。Angular中的SessionStorage:訪問和更改應用程序中的存儲(緩存工廠,ngStorage)
我需要訪問它的部分代碼是這樣的:
app.run(function($rootScope, $state, $window, $alert, AuthService) {
$rootScope.$on(
,並在我的控制器,並在我的HTML,無處不在。我沒有找到解決方案。我試着服務(與ngStorage)
appServices.factory('AuthService', function ($rootScope, $localStorage){
return {
set: function(entidy, value) {
$storage = $localStorage;
$storage[entidy] = value;
}
get: function(entidy) {
if (typeof $rootScope.$storage != 'undefined') {
return $rootScope.$storage[entidy];
}
}
}
});
寫我的服務是這樣的:
AuthService.set(email, '[email protected]');
AuthService.set(token, '12345');
AuthService.set(auth, true);
不過的getter/setter很爛,這不是20世紀90年代。以上也不起作用。有任何想法嗎?我需要一個服務來緩存sessionStorage中的數據嗎?請記住,我需要在根作用域(以及其他任何地方)訪問它。
因此$scope.$storage = $localStorage
將失敗。
是的,這個問題是ngStorage特有的,但Angular自己的cacheFactory的設置應該是一樣的。
Plunkr:http://plnkr.co/edit/GQomw4LqHCi7MJZTHZIG?p=preview
嘗試''email'':'AuthService.set('email','[email protected]');' – 2014-09-30 09:44:50
添加了一個Plunkr。 – nottinhill 2014-09-30 10:34:04