AngularJS循环访问http get req以找到正确的url

时间:2015-07-21 07:52:41

标签: javascript angularjs http asynchronous

所以我正在开发一个项目,我正在制作一个http请求,其角度大约为1500个URL,寻找与我所拥有的条件匹配的json(只有1个URL匹配)。我目前有一个实现有时工作(但我不假设是因为它的请求是异步的,虽然它可能只是一个错误??)。我仍然对角度有点新意,所以我不确定我是否正确地执行它所以我完全可以改变代码!

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”,无论是对还是错。

2 个答案:

答案 0 :(得分:0)

我建议您使用$q angularjs的承诺来完成任务,要么你可以连续一次检查一个网址(如果你问我,请慢一点),或者通过并行请求一次获得所有结果。下面,我做了后者的粗略实施

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
}

答案 1 :(得分:0)

你是对的,如果你没有正确处理你的变量,你可能会因同步http调用而遇到麻烦。以下是使用同步http调用实现相同功能的代码段。

&#13;
&#13;
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);
&#13;
&#13;
&#13;