【问题标题】:AngularJS looping through http get req to find correct urlAngularJS 循环通过 http get req 找到正确的 url
【发布时间】:2015-10-10 13:34:04
【问题描述】:

所以我目前正在做一个项目,我正在发出一个角度为大约 1500 个 URL 的 http 请求,以寻找与我所拥有的条件匹配的 json(只有 1 个 URL 会匹配)。我目前有一个有时可以工作的实现(但我假设它不是确定性的,因为它的请求是异步的,尽管它可能只是一个错误??)。我对 Angular 还是有点陌生​​,所以我不确定我是否做得正确,所以我愿意完全改变代码!

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”。

【问题讨论】:

  • 我不明白为什么你必须使用$http.get() 如果你已经有数组中的 URL 并且想将它与某些东西进行比较?另外,您能否展示一下您的列表的结构?
  • @DanielB,他想将获取的数据的内容与某些东西进行比较,而不是 url 本身。
  • 这就是我最初的想法,但是问题和代码的措辞和命名方式使它听起来不一样。不过,很高兴看到 URL 数组的结构。
  • 结构基本上是一个 json 对象列表名称:____,列表:(来自同一站点的 url 列表)

标签: javascript angularjs http asynchronous


【解决方案1】:

我建议您使用 angularjs 的$q promise 来完成任务,您可以一次连续检查一个 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;
            }
        })
    });
}

编辑:

一次检查一个 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...
        });
    });

    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
}

【讨论】:

    【解决方案2】:

    你是对的,如果你没有正确处理你的变量,你可能会因为同步的http 调用而遇到麻烦。这是一个使用同步 http 调用实现相同目的的 sn-p。

    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);

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-13
      • 1970-01-01
      • 1970-01-01
      • 2015-03-01
      • 2017-06-14
      • 1970-01-01
      相关资源
      最近更新 更多