2015-07-21 39 views
2

所以我目前正在一個項目,我正在做一個HTTP請求角度約1500個URL尋找與我有條件相匹配的JSON(只有1的URL會匹配)。我目前有一個有時候可以工作的實現(但是我沒有確定性,因爲它的請求是異步的,儘管它可能只是一個bug而已)。我還是有點新的角,所以我不知道我是否正確地做到了這一點,所以我打算完全改變代碼!AngularJS通過http循環獲取req找到正確的網址

this.matchingurl; 
this.data; 
this.findUrl = function(condition) { 
    var that = this; 
    for (var i = 0; i <= ; i++) { 
    // this is just looping through the url list 
    for (var i = 0; i < urlList.length; i++) { 
     for (var j = 0; j < urlList[i]['list'].length; j++) { 
     this.url = 'http://' + urlList[i]['list'][j] + restofurl; 
     var tempUrl = urlList[i]['list'][j]; 
     $http.get(this.url).success(function(data) { 
      if (condition is met in data) { 
      that.matchingurl = tempUrl; 
      return; 
      } 
     }) 
     .error(function(data){ 
      // error handling 
     }); 
     } 
    } 
    } 
} 

TLDR:matchingUrl是不是我所期望的?仍然進入「條件」循環,但不吐出正確的URL。總是給我任何子列表相同的「url」,對或錯。

+0

我不明白你爲什麼要使用'$ http.get()'如果你已經在陣列中的網址,並希望把它比作什麼?另外,你能否展示你的列表是如何構建的? –

+0

@DanielB,他想將獲取的數據的內容與某個東西進行比較,而不是url本身。 – Fissio

+1

這就是我最初的想法,但問題和代碼的表述方式和命名方式使其聽起來很彆扭。儘管如此,看到URL數組的結構會很好。 –

回答

0

我建議你使用$q承諾angularjs來完成任務,或者你可以連續檢查一個url(如果你問我要慢一些),或者一次請求並行所有結果。下面,我已經做了原油實現後者

this.findUrl = function(condition) { 
    var urls =[], self = this, oUrl; // collect all the urls 
    urlList.forEach(function(list){ 
     list.forEach(function(url){ 
      oUrl.push(url); 
      urls.push('http://' + url + restofurl); // not sure where you are getting this restofurl from... 
     }); 
    }); 

    $q.all(urls.map(function(url){ 
     return $http.get(url); // returns promise for each url, thus mapping all urls to promise. 
    })).then(function(datas){ 
     datas.some(function(data, i){ 
      if(data == condition){ // change as per requirement 
       self.matchingurl = oUrl[i]; 
       return true; 
      } 
     }) 
    }); 
} 

編輯的:

同樣的事情做了檢查一個網址進行查詢:

this.findUrl = function(condition) { 
    var urls =[], self = this, oUrl; // collect all the urls 
    urlList.forEach(function(list){ 
     list.forEach(function(url){ 
      oUrl.push(url); 
      urls.push('http://' + url + restofurl); // not sure where you are getting this restofurl from... 
     }); 
    }); 

    function check(i){ 
     function fail(){ // move to check the next url in the array 
      i++; 
      if(i<urls.length) return check(i); 
      console.log('none of the urls are matching');     
     } 

     return http.get(urls[i]).then(function(data){ 
      if(data == condition){ // change as per requirement 
       self.matchingurl = oUrl[i]; 
      }else{ 
       fail(); 
      } 
     }).catch(fail); 
    } 
    check(0); // start the chain 
} 
0

你是對的,這個你可以如果您沒有正確處理您的變量,則由於同步http調用而陷入麻煩。這是一個使用同步http調用實現相同的片段。

this.matchingurl; 
 
this.data; 
 
this.findUrl = function(condition, i, j) { 
 
     var that = this; 
 
     this.url = 'http://' + urlList[i]['list'][j] + restofurl; 
 
     var tempUrl = urlList[i]['list'][j]; 
 
     $http.get(this.url).success(function(data) { 
 
      if (condition is met in data) { 
 
      that.matchingurl = tempUrl; 
 
      return; 
 
      } 
 
      else{ 
 
      if(urlList[i]['list'].length > j + 1){ 
 
       j++; 
 
      } 
 
      else{ 
 
       if(urlList.length > i+1){ 
 
       i++; 
 
       j=0; 
 
       } 
 
       else{ 
 
       return; 
 
       } 
 
      } 
 
      this.findUrl(condition, i, j); 
 
      } 
 
     }) 
 
     .error(function(data){ 
 
      // error handling 
 
     }); 
 
     } 
 
    } 
 
    } 
 
} 
 

 
this.findUrl(condition, 0, 0);