Javascript AnulgarJS-服务中的对象参数未更改
我在服务方面遇到了一些问题 基本上,我有一个服务,作为用户特定参数的常量类,我将这些存储在从cookie读取值的变量中 这是我的服务:Javascript AnulgarJS-服务中的对象参数未更改,javascript,angularjs,cookies,service,constants,Javascript,Angularjs,Cookies,Service,Constants,我在服务方面遇到了一些问题 基本上,我有一个服务,作为用户特定参数的常量类,我将这些存储在从cookie读取值的变量中 这是我的服务: shoutApp.service('userConfig', function (userService, config) { this.USER_ID = $.cookie('shoutUserObj')._id; this.USER_USERNAME = $.cookie('shoutUserObj').username; this.USER_PASSWOR
shoutApp.service('userConfig', function (userService, config) {
this.USER_ID = $.cookie('shoutUserObj')._id;
this.USER_USERNAME = $.cookie('shoutUserObj').username;
this.USER_PASSWORD = $.cookie('shoutUserObj').password;
this.USER_JOINDATE = $.cookie('shoutUserObj').joinDate;
this.USER_TWITTER = $.cookie('shoutUserObj').twitter;
this.USER_FACEBOOK = $.cookie('shoutUserObj').facebook;
this.USER_GOOGLE = $.cookie('shoutUserObj').google;
this.refreshUserObj = function (callback) {
console.log("Starting refresh");
//This function retrieves new user data
userService.requestUserObj($.cookie('shoutUserObj')._id, function (code, userObj) {
if (code === config.CODE_AJAX_SUCCESS) {
//Here I delete the old cookie and set a new one with the same name
$.removeCookie('shoutUserObj');
$.cookie.json = true;
$.cookie('shoutUserObj', userObj, { path: '/', expires: 7 });
//When I log the new values of the cookie at this point they changed
}
});
}
});
我还尝试将它们存储在对象中,但每次更改参数时,它们只会在类内更改(例如,当我在刷新函数中注销新变量时,它们会更改,但当我尝试通过返回值从控制器访问它们时,它们不会更改)
控制器示例:
shoutApp.controller('profileController', function ($scope, config, userConfig) {
console.log("Username: " + userConfig.USER_USERNAME);
//This value allways stays the same, even when I changed the cookie
});
我的目标是在使用用户服务的所有控制器中获得更改的参数,我如何实现这一点
我真的非常感谢你的帮助 服务是单例的,因此
userConfig
属性的值将始终具有初始化服务时的值
在服务中,如果每次都要检索新值,请使用函数
this.getUSER_USERNAME = function() {
return $.cookie('shoutUserObj').username;
}
而不是财产
this.USER_USERNAME = $.cookie('shoutUserObj').username;
那么示例控制器将是:
shoutApp.controller('profileController', function ($scope, config, userConfig) {
console.log("Username: " + userConfig.getUSER_USERNAME());
//This will return the current value of the cookie
});
您的问题/代码示例没有提供足够完整的设置图片。请看一看并考虑更新你的问题(可能是一个赌注的岔口),这能更好地证明这个问题。