簡體   English   中英

在RootCtrl Angular $ http中等待promise

[英]Wait for promise in RootCtrl Angular $http

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

但是,當我嘗試在子控制器之一中調用此方法時,就像這樣……

var access = $scope.checkAccess();

它告訴我access未定義。

我在這里做錯了什么?

這是服務調用的樣子。

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

您忘記了實際返回promise對象。

干得好:

$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