2017-04-06 34 views
0
 $scope.observer_vel_data = function(){ 
     $scope.showOverlay('loadRefPubVel'); 
     $http({ 
      //First http post request 
      method:'POST', 
      url:'/api/observer_vel_data', 
      data:$scope.payload_array, 
     }).then(function successCallback(response){ 
      console.log('API Endpoint: vel data success!'); 
      //Second post request is made in the method call below 
      $scope.sentiment_var = $scope.observer_send_sentiment(); 
      $scope.vel_var = response.data.velocity1; 
     }, function errorCallback(response){ 
      // console.log(response); 
      $scope.addAlert({ 
       type: 'danger', 
       msg: 'API call failed' 
      }); 
     }).finally(function(){ 
      console.log("hello"); 
      console.log($scope.sentiment_var); 
      //graph is rendered 
      $scope.update_velocity($scope.vel_var,$scope.sentiment_var); 
      $scope.hideOverlay('loadRefPubVel'); 
     }); 
    }; 

所以我想呈現使用來自兩個不同和獨立發佈請求數據的圖形。但是,在來自第二個post請求的數據到達之前調用graph命令。我怎樣才能解決這個問題 ?發佈的代碼中提到了發佈帖子並渲染圖的命令。在角度相同的方法多個帖子請求

 $scope.observer_send_sentiment = function(){ 
     // $scope.showOverlay('loadRefSentiment'); 
     var data = { 
      "angularGroups":$scope.groups 
     }; 
     // console.log(data); 
     $http({ 
      method:'POST', 
      url:'http://localhost:9612/sentiment_velocity', 
      data:data 
     }).then(function successCallback(response){ 
      var data = response.data; 
      var json_obj = JSON.parse(data.replace(/\'/g,'"')); 
      var sentiments = json_obj["sentiments"]; 
      // console.log(sentiments); 
      $scope.update_sentiment(sentiments); 
      console.log(sentiments); 
      return sentiments; 
     }, function errorCallback(response){ 
      var errmsg = response.statusText; 
      console.log(response); 
      $scope.addAlert({ 
       type: 'danger', 
       msg: 'API call failed (sentiment basic)' + errmsg, 
      }); 
     }).finally(function(){ 
      // $scope.hideOverlay('loadRefSentiment'); 
     }); 
    }; 
+0

這是第二次調用:「$ scope.sentiment_var = $ scope.observer_send_sentiment();」?如果是這樣,那麼請包括實際的http請求代碼。 – alphapilgrim

+0

@alphapilgrim新增 –

+0

這兩個請求是否都生活在同一個控制器/工廠? – alphapilgrim

回答

1

如果我理解正確,只希望在第二個請求結束後執行finally(...)中的代碼。

爲了強制執行此操作,您需要鏈接HTTP請求承諾,這意味着您需要從第一個請求的成功處理程序返回第二個HTTP請求的承諾。你的代碼看起來應該或多或少是這樣的:

$scope.observer_vel_data = function(){ 
    $scope.showOverlay('loadRefPubVel'); 
    $http({ 
     method:'POST', 
     url:'/api/observer_vel_data', 
     data:$scope.payload_array, 
    }).then(function successCallback(response){ 
     console.log('API Endpoint: vel data success!'); 
     $scope.vel_var = response.data.velocity1; 
     return $scope.observer_send_sentiment(); 

    }).catch(function errorCallback(response) { 
     //This catch will handle errors from both HTTP requests 
     $scope.addAlert({ 
      type: 'danger', 
      msg: 'API call failed' 
     }); 
    }) 
    .finally(function(){ 
     console.log("hello"); 
     console.log($scope.sentiment_var); 
     //graph is rendered 
     $scope.update_velocity($scope.vel_var,$scope.sentiment_var); 
     $scope.hideOverlay('loadRefPubVel'); 
    }); 
}; 

$scope.observer_send_sentiment = function() { 
    return $http({...}).then(function(response) { 
     //process the response 
     ... 
     $scope.sentiment_var = parsedResponseData; 
    }); 
}; 

注意,finally回調都會被執行,而不管是否發生錯誤或沒有。如果您只想在遇到錯誤時執行其中的一部分操作,請改爲添加另一個.then(function() {...})

編輯︰現在我們可以看到observer_send_sentiment做什麼,它可能是有道理的,你堅持.then(function successCallback() {...}, function errorCallback() {...})語法,併爲每個請求保持單獨的錯誤回調。請記住,如果您添加了另一個then區塊,並且您希望承諾鏈中存在錯誤以防止執行更多.then(function() {...})區塊,則應在errorCallback s的末尾添加return $q.reject(response)。不使用q.reject從錯誤回調中用.then(function successCallback() {...}, function errorCallback() {...})語法渲染承諾解決而不是拒絕