2016-08-30 86 views
2

正如標題所說,我想發送$ http.get請求到服務器,直到返回的結果滿足條件。

這是我的嘗試:

var searchComplete = false; 
    var sendGetReq = true; 

    while(!searchComplete) // #2 and then jumps here 
    { 
     if(sendGetReq) 
     { 
     sendGetReq = false; // #1 execution reaches here 
     $http.get('/user/getCondition', {params: {condition: $scope.condition}). 
     success(function (condition, status, headers, config) { 
     ... 
     ... 
     if(condition) 
     { searchComplete = true; } 
     else 
     { sendGetReq = true; } 
    } 
} 

然而,$ http.get請求永遠不會。該聲明從未達到。當我調試時,我看到執行將會到達sendGetReq=false,然後跳轉到while(!searchComplete)。 GET請求永遠不會被髮送。

會有人請解釋:

  1. 爲什麼執行從未達到GET請求並跳回
  2. 什麼是做這種連續GET請求

謝謝你的推薦方式。

+2

使用''.then''',而不是'''.success''',你應該使它成爲一個遞歸函數。不要期待一段時間和一起承諾的完美行爲。讓你自己成爲一旦這個承諾已經返回就自動調用的函數。 –

回答

3

爲什麼不把它一起在函數內部,並呼籲本身,如果條件是滿足?像這樣的,你應該使用。那麼,不.success:

$scope.load = function(){ 
    $http.get('/user/getCondition', {params: {condition: $scope.condition}). 
    then(function (condition, status, headers, config) { 

    if (condition){ 
     $scope.load(); 
    } 

    }); 
} 

希望它可以幫助=)

+0

'!condition'這是正確的 – gianlucatursi

2

請勿使用while (true),特別是在JS中。

JS在一個線程中工作。 $http.get永遠不會運行,因爲你的主線程停滯在你的無限循環,並沒有給其他線程運行的機會。

更新

function getHttpPromise() { 
    return $http.get('/user/getCondition', {params: {condition: $scope.condition}). 
     success(function (condition, status, headers, config) { 
     ... 
     ... 
     if(condition) 
      return true;//or any result you want to resolve 
     else 
      return getHttpPromise(); 
    } 
} 

getHttpPromise函數返回的承諾,這將在您的condition來解決,它將繼續與其他等待的情況下跟隨GET請求。

要使用它,請運行以下代碼。

getHttpPromise().then(function (result) { 
    // Will be called only when condition will be successful 
}); 
1

試試這個在您的控制器注入$interval

var startInterval = $interval(function(){ 
    var searchComplete = false; 
    var sendGetReq = true; 

     sendGetReq = false; // #1 execution reaches here 
     $http.get('/user/getCondition', {params: {condition: $scope.condition}). 
     success(function (condition, status, headers, config) { 
     if(condition) 
     { searchComplete = true; 
      $scope.stopInterval(); 
     } 
     else 
     { sendGetReq = true; } 
     }) 

},1000) 

$scope.stopInterval = function(){ 
    $interval.cancel(startInterval) 
} 
2

$http.get是異步調用,它會擊中成功或錯誤(包括已被棄用,然後使用)函數從服務器響應。當功能到達該線

sendGetReq = false; // #1 execution reaches here 

它使$http.get呼叫(這是異步),然後將其移動控制下一次迭代。這是異步調用的主要目的。

如果您想連續呼叫,您可以在函數中編寫$http.get呼叫,並使用Angular JS的interval服務調用該函數。

$interval

$scope.func = function(){ 
    if(condition) 
    { 
     $http.get.... 
    }  
} 

$interval($scope.func, 1000) 

,其中1000以毫秒爲單位的時間間隔後,該功能被稱爲

+1

''超時'你只能調用一次函數。也許你需要'$ interval',但無論如何不是一個好的解決方案。你應該使用then(成功)遞歸調用你的函數 – gianlucatursi

+0

@gianlucatursi編輯我的答案爲$ interval –

1

安德魯說應該是遞歸的,而且你的情況。這時使用。爲了讓遞歸封裝一個函數內部$ http服務

.controller('YourController', function($http) { 
this.sendGetReq= function(data) { 
    $http.get('/user/getCondition', {params: {condition: data.condition}). 
    .then(function(res) { 
     if (res.condition) { 
     //finish your search 
     } else { 
     //set newdata somehow 
      this.sendGetReq(newdata); 
     } 
    } 
}; 

});