简体   繁体   English

在RootCtrl Angular $ http中等待promise

[英]Wait for promise in RootCtrl Angular $http

I have a method in my RootCtrl that makes a call to my http api, and returns the result. 我的RootCtrl中有一个方法可以调用我的http api,并返回结果。

$scope.checkAccess = function(){
    var result = MyService.me();
    result.then(function(response){
        console.log(response);
        if (response.data != 'false'){
            return true;
        }
        else{
            return false;
        }
    });
}

But when I try to call this method in one of the child controllers, like so... 但是,当我尝试在子控制器之一中调用此方法时,就像这样……

var access = $scope.checkAccess();

it tells me access is undefined. 它告诉我access未定义。

What am I doing wrong here? 我在这里做错了什么?

Here is what the Service call looks like. 这是服务调用的样子。

me: function() {
    return $http({
        url: 'http://localhost:5000/api/me',
        method: 'GET'
      });
}

You forgot to actually return the promise object. 您忘记了实际返回promise对象。

Here you go: 干得好:

$scope.checkAccess = function(){
    var result = MyService.me();
    return result.then(function(response){
        console.log(response);
        if (response.data != 'false'){
            return true;
        }
        else{
            return false;
        }
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM