使用getter函数返回undefined从服务角度揭露的对象 [英] Expose an object from an Angular service using Getter function return undefined

查看:92
本文介绍了使用getter函数返回undefined从服务角度揭露的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是code:

authServ.getUser()返回{}(空对象,对应于该变种的声明),从无处不在,即使我已经到返回语法根据这个[问题]作出的修订后[1]

authServ.getUser() returns {} (an empty object, which corresponds to the declaration of this var), from everywhere, even after the revision that I have made to the return syntax according to this [question][1].

任何人都可以请告知有什么问题?我没有看到一个原因,它不应该工作

Can anyone please advise what is the issue?, I don't see a reason why it shouldn't work

app.factory('authService', function($http){
    var authServ = {};
    var currentUser = {};
        authServ.authUser = function(){
            return $http.head('/users/me', {withCredentials: true});
        },
        authServ.getUser =  function(){
            return currentUser;
        },
        authServ.setCompany =  function(companyId){
            currentUser.company = companyId;
        }
        authServ.loadCurrentUser = function(){
            $http.get('/users/me', {withCredentials: true}).
            success(function(data, status, headers, config){
                console.log(data);
                currentUser.company = currentUser.company ? currentUser.company : data.main_company;
                currentUser.companies = [];
                for(var i in data.roles){
                    currentUser.companies.push(data.roles[i]['company_name']);
                    if(data.roles[i]['company'] == currentUser.company)
                        currentUser.role = data.roles[i]['role_type'];
                }
                console.log(currentUser);
            }).
            error(function(data, status, headers, config){
                currentUser.role = 'guest';
                currentUser.company = 1;
            });
        }

        return authServ;
});

工作code:

run(function($rootScope, $location, $http, authService){
$rootScope.$on("$routeChangeError", function(event, current, previous, rejection){
    if(rejection.status == 401)
        $location.path('/login');
})
authService.loadCurrentUser().then(function(){
    console.log(authService.getUser());
});


});
app.factory('authService', function ($http) {
    authServ = {};
    that = this;
    that.currentUser = {};
    authServ.authUser = function () {
        return $http.head('/users/me', {
            withCredentials: true
        });
    },
    authServ.getUser = function () {
        return that.currentUser;
    },
    authServ.setCompany = function (companyId) {
        that.currentUser.company = companyId;
    },
    authServ.loadCurrentUser = function () {
        return $http.get('/users/me', {
            withCredentials: true
        }).
        success(function (data, status, headers, config) {
            console.log(data);
            that.currentUser.company = that.currentUser.company ? that.currentUser.company : data.main_company;
            that.currentUser.companies = [];
            for (var i in data.roles) {
                that.currentUser.companies.push(data.roles[i]['company_name']);
                if (data.roles[i]['company'] == that.currentUser.company) that.currentUser.role = data.roles[i]['role_type'];
            }
            console.log(that.currentUser);
        }).
        error(function (data, status, headers, config) {
            that.currentUser.role = 'guest';
            that.currentUser.company = 1;
        });
    }
    return authServ;
});

小提琴:
http://jsfiddle.net/V9Ex6/1/

推荐答案

关闭问题。尝试

app.factory('authService', function($http){
    var authServ = {};
    that = this; //that captures the current closure
    this.currentUser = {};
    authServ.getUser =  function(){
        return that.currentUser;
    },

和变化 loadCurrentUser 来访问使用 that.currentUser 变量。

And change loadCurrentUser to access to the variable using that.currentUser.

修改

authService.loadCurrentUser();
console.log(authService.getUser());

用户无法保证,因为 loadCurrentUser 来打印出异步加载用户。您应该更改 loadCurrentUser 来采取一个回调函数,以获得用户的价值。

The user is not guaranteed to be printed out since loadCurrentUser loads the user asynchronously. You should change loadCurrentUser to take a callback function in order to get the user value.

希望它帮助。

这篇关于使用getter函数返回undefined从服务角度揭露的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆