【问题标题】:Angular $http with $ in a forEach loop在 forEach 循环中带有 $ 的 Angular $http
【发布时间】:2016-08-21 12:03:54
【问题描述】:

我有一个返回以下对象的工厂

返回{

 getCuisines: function(cuisineNames){
      if (typeof cuisineNames !== 'string' && typeof cuisineNames !== undefined) {
        throw new Error('Cusine type should be a string or should not have value');
      }
      cuisineNames = cuisineNames || options.term;
      cuisineNames = cuisineNames.split(',');


      var promises=[];
      cuisineNames.forEach(function(cuisine){
        var defered = $q.defer();
        options.term =  cuisine + ' '+ 'food';
        $http.get(url,{params: options}).success(function(data){
          defered.resolve(data);
          localStorageService.setDishes(cuisine,data.businesses);
          console.log("Cuisine ="+cuisine);

          console.log(data);
        });
        promises.push(defered.promise);
      });
      return $q.all(promises);
    }
  };

我从控制器调用这个工厂

MyFactory.getCuisines('American,Indian,Italian,Mexican,Japanese,Korean').then(function(data){
      console.log(data);
    });

应用程序执行时。我把所有的菜都当作韩国菜。最后一个参数通过了。

更新 1:

我将我的代码更新为以下代码。删除了 $q。将 $http 推送到 promises 数组仍然无法正常工作。相同的反应。我检查了我的服务器,所有对服务器的调用都是针对韩国食品的。

return {
  getCuisines: function(cuisineNames){
      if (typeof cuisineNames !== 'string' && typeof cuisineNames !== undefined) {
        throw new Error('Cusine type should be a string or should not have value');
      }

      cuisineNames = cuisineNames || options.term;
      cuisineNames = cuisineNames.split(',');


      var promises=[];
      cuisineNames.forEach(function(cuisine){
        options.term =  cuisine + ' '+ 'food';
        console.log(options.term);  //I see different cuisine name here
        promises.push($http.get(url,{params: options}).success(function(data){
          localStorageService.setDishes(cuisine,data.businesses);
               console.log(data);
        }));
      });
      return $q.all(promises);
    }
  };

【问题讨论】:

  • 首先,为什么$q服务为$http服务? http 服务默认返回一个承诺。看到这个问题:stackoverflow.com/questions/36631601/…
  • 小心,一次只能有6个ajax调用(导航器限制)。您最好使用 1 次调用来获得所有结果。

标签: angularjs


【解决方案1】:

你只会得到韩语,因为它是迭代中的最后一个并且覆盖了其他承诺。您希望 push 承诺 $http.get 请求,而不是您为捕获 $http 结果而定义的延迟器。

【讨论】:

    【解决方案2】:

    几件事:

    1. 正如@topless 所提到的,您需要将 $http 承诺推送到 你的承诺数组
    2. $q.all() 应该有成功方法 '.then' 来捕获 您的服务调用的结果,例如检查此 SO link

    【讨论】:

      【解决方案3】:

      嗯,很难看不到网络活动,但你没有从 get $http 调用中返回,尝试使用该语法:

      getCuisines: function(cuisineNames){
          if (typeof cuisineNames !== 'string' && typeof cuisineNames !== undefined) {
              throw new Error('Cusine type should be a string or should not have value');
          }
      
          cuisineNames = cuisineNames || options.term;
          cuisineNames = cuisineNames.split(',');
      
          var promises = cuisineNames.map(function(cuisine) {
              options.term =  cuisine + ' '+ 'food';
              console.log(options.term);
              return $http.get(url,{params: options})
                          .then(function(data){
                              localStorageService.setDishes(cuisine,data.businesses);
                              console.log(data);
                              return data;
                          });
          });
      
          return $q.all(promises);
      }
      

      【讨论】:

      • 我认为 '.then' / '.success' 取决于使用的角度版本。我在两种情况下都尝试了您的解决方案,我得到了相同的响应。 console.log(options.term); 记录不同的美食名称,但只为韩国人拨打电话
      猜你喜欢
      • 2015-06-16
      • 1970-01-01
      • 2017-08-07
      • 2021-11-24
      • 2017-02-28
      • 1970-01-01
      • 1970-01-01
      • 2020-07-26
      • 2021-05-28
      相关资源
      最近更新 更多