0

我有一个服务可以获取包含他或她的首选语言的用户信息,我想在我的控制器中使用它来在用户登录时更改我的应用程序的语言,
但是当我执行“$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);

    });
}());

用户服务:

(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);
};

}());

用户信息:

(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 函数时,它确实很神奇,现在我只需要让它工作应用程序的初始加载。

谢谢你检查它 J。

4

0 回答 0