我有一个获取包含他或她的首选语言的Userinfo的服务,我想在我的控制器中使用它来在用户登录时更改我的应用程序的语言,
但是,当我执行'$ scope.User.locale'时,它应该返回首选语言,我一直未定义。
我的代码是:
(function () {
'use strict';
var controllers = angular.module('portal.controllers');
controllers.controller('mainController', function mainController($scope ,UserService, NavigationService, $translate, localStorageService) {
localStorageService.clearAll();
$scope.User = UserService.getUserinfo();
$scope.setLang = function (langKey) {
$translate.uses(langKey);
$.removeCookie(Constants.cookie_locale);
var domain = document.domain;
$.cookie(Constants.cookie_locale, langKey, {path: "/", domain: domain});
};
$scope.logout = function(){
NavigationService.logout();
};
console.log($scope.User);
console.log($scope.User.locale);
$translate.uses($scope.User.locale);
});
}());
UserService:
(function(){
'use strict';
var userServices = angular.module('portal.services');
userServices.factory('UserService', ['UserInfo','localStorageService', function(UserInfo, localStorageService){
return new UserService(UserInfo, localStorageService);
}]);
function UserService(UserInfo, localStorageService){
this.UserInfo = UserInfo;
this.localStorageService = localStorageService;
}
UserService.prototype.getUserinfo = function(){
var userinfo = this.localStorageService.get(Constants.key_userinfo);
if(userinfo !== null){
return userinfo;
} else {
return this.UserInfo.query($.proxy(function(data){
this.localStorageService.add(Constants.key_userinfo, JSON.stringify(data));
}, this));
}
};
UserService.prototype.clearUserInfo = function(){
this.localStorageService.remove(Constants.key_userinfo);
};
}());
USERINFO:
(function(){
'use strict';
var data = angular.module('portal.data');
data.factory('UserInfo', function($resource){
return $resource('/users-rs/api/userinfo',{}, {
query: {method: 'GET'}, isArray: false}
);
});
}());
$ scope.User确实将正确的功能绑定到我想要的元素上,所以我知道它在那里的某个地方:p,当页面加载并且调用setLang函数时它也是魔术,现在我只需要获取它正在处理应用程序的初始加载。
检查它的Thx 学家