返回$rootScope函数中的对象(AngularJS)

Return Object in $rootScope Function (AngularJS)

本文关键字:对象 AngularJS rootScope 函数 返回      更新时间:2023-09-26

我正试图从AngularJS中名为retrieveUser()的$rootScope函数返回一个对象。返回对象。我已经对$http成功时运行的函数的响应运行了console.log()。这是我的$rootScope函数:

    $rootScope.retrieveUser = function() {
        var apiUrl = "http://104.251.218.29:8080";
        if($cookies.get('tundraSessionString')) {
            var cookie = $cookies.get('tundraSessionString');
            $http({
                method: "POST",
                url: apiUrl + "/api/master/v1/auth/checkauth",
                data: "sessionString=" + cookie,
                headers: {
                    'Content-Type' : 'application/x-www-form-urlencoded;',
                    'Cache-Control': 'no-cache'
                }
            }).then(function mySuccess(response) {
                if(response.data.event == "error") {
                    window.location = "/auth/logout";
                } else {
                    return response.data;
                }

            })
        } else {
            window.location = "/auth/login";
        }
    };

使用这个方法,我在我的控制器中访问它,比如这个(和console.log()只是为了测试我的工作):

vm.user = $rootScope.retrieveUser();
console.log($rootScope.retrieveUser());

但是,我还没有让它发挥作用。我尝试在$rootScope函数的数组中指定特定对象。我知道它在运行,因为当它运行时,我有$rootScope在整合一些东西,它显示了$http请求响应的console.log()。它看起来像这样:

Object {event: "success", table: Object}
event:"success"
table:Object
__proto__:Object

然而,当我用函数$rootScope.revegeUser()控制台.log()vm.user时,即使该函数应该返回对象,我也只是收到"未定义"。

几天来,我一直在思考这个问题,读了一些关于函数/对象的文章,但我仍然无法理解。我们还有两天。

试试这个:

        if($cookies.get('tundraSessionString')) {
            var cookie = $cookies.get('tundraSessionString');
            //return a promise
            return $http({
                method: "POST",
                url: apiUrl + "/api/master/v1/auth/checkauth",
                data: "sessionString=" + cookie,
                headers: {
                    'Content-Type' : 'application/x-www-form-urlencoded;',
                    'Cache-Control': 'no-cache'
                }
            }).then(function mySuccess(response) {
                if(response.data.event == "error") {
                    window.location = "/auth/logout";
                } 
                else {
                    return response.data;
                }

            })
        } 
        else {
            window.location = "/auth/login";
        }

$rootScope.retrieveUser().then(function(user){vm.user = user;})

设置cookie时,您从retrieveUser返回的是$http返回的内容,这是一个承诺。试试这个:

$rootScope.retrieveUser().then(function(user){vm.user = user;})

retrieveUser fn不返回您的数据:)$http是异步函数,您应该阅读promise

function handleUser(user){
//do something
}
function retrieveUser(callback){
  $http({...}).then(function(response){
    callback(response.data.user);
  });
}
//how to use it:
retrieveUser(handleUser);

但首先,你可能需要一个获取一些数据的服务,而不是使用$rootScope

其次,您可以在script标签中传递模板中的用户然后你不需要另一个http请求,用户将在全球范围内可用

<script>var user=<?php echo json_encode($user);?></script>