【问题标题】:Return Object in $rootScope Function (AngularJS)在 $rootScope 函数 (AngularJS) 中返回对象
【发布时间】:2016-07-20 21:58:40
【问题描述】:

我正在尝试从 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.retrieveUser() console.log() vm.user 时,即使该函数应该返回对象,我也只是收到“未定义”。

几天来我一直在努力解决这个问题,阅读了一些关于函数/对象的文章,但我仍然无法弄清楚这一点。两天了。

【问题讨论】:

    标签: javascript angularjs json


    【解决方案1】:

    试试这个:

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

    【讨论】:

    • 这成功了!从现在开始,我会记得使用 promise 方法。我是 Angular 的新手。我构建的第一个应用程序。但我已经走了很远没有打嗝。感谢您的帮助!
    【解决方案2】:

    设置 cookie 时从 retrieveUser 返回的内容是 $http 返回的内容,即 promise。试试这个:

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

    【讨论】:

    • TypeError: Cannot read property 'then' of undefined 在 Chrome 控制台日志中。
    • retrieveUser 函数在使用 .then 方法时应该返回一个承诺。您可以返回 $http.get (它也使用了一个承诺)
    • 是啊哎呀我习惯了coffeescript,你还需要把它改成return $http({...
    【解决方案3】:

    retrieveUser fn 不会返回您的数据 :) $http 是异步函数,你应该阅读有关 promises 的内容

    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>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-06
      • 2023-03-22
      • 1970-01-01
      • 1970-01-01
      • 2014-03-11
      • 1970-01-01
      相关资源
      最近更新 更多