我正在使用 angularjs 开发 Web 应用程序。我在整个应用程序中都需要用户的电子邮件地址,如何存储它以供进一步使用?
目前我正在做的是:
我创建了一个用户服务
'use strict';
var module = angular.module('moduleName');
module.factory('UserService',['$resource',function($resource){
var currentUserEmail = null;
return {
getUserResource : function(){
return $resource('users',{email:'@email'});
},
saveCurrentUserEmail : function (email){
currentUserEmail = email;
},
getCurrentUserEmail : function (){
return currentUserEmail;
}
};
}]);
登录时,我使用用户服务存储用户电子邮件地址:
UserService.saveCurrentUserEmail($scope.username);
现在,当我转到另一个视图时,在控制器中,用户电子邮件是我所期望的。
console.debug("Current User email "+UserService.getCurrentUserEmail());
打印用户的电子邮件地址
但是当我用 (ctrl+f5) 刷新页面时。那么当前用户电子邮件为空。
Current User email null
我做错了什么,或者我应该怎么做才能保存这个电子邮件地址以供进一步使用?