使用Getter函数返回undefined从Angular服务公开对象

时间:2013-08-07 12:58:52

标签: javascript angularjs angularjs-directive

这是代码:

authServ.getUser()返回{}(一个空对象,对应于此var的声明),来自任何地方,即使在我根据此[问题] [1]对返回语法进行的修订之后

任何人都可以告诉我们这是什么问题吗?我没有看到它不应该起作用的原因

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

工作代码:

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/

2 个答案:

答案 0 :(得分:2)

请注意,我还没有测试过它:)

app.factory('authService', function($http){
    return {
        authUser: function(){
            return $http.head('/users/me', {withCredentials: true});
        },
        getUser:  function(){
            return currentUser;
        },
        setCompany:  function(companyId){
            currentUser.company = companyId;
        },
        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;
            });
        }
     }
});

答案 1 :(得分:2)

关闭问题。尝试

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以访问变量。

修改

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

由于loadCurrentUser异步加载用户,因此无法保证打印出用户。您应该更改loadCurrentUser以获取回调函数以获取用户值。

希望它有所帮助。