Angular承诺间歇性地工作

时间:2016-08-27 02:20:08

标签: javascript angularjs asynchronous

我的应用程序请求以promise形式返回的数据。我循环并发出10个请求将promises推送到数组,然后$q.all数组。我.then更新ng-repeat的模型,数据显示在视图中。

一切正常但当我点击一个按钮再次使用新请求运行操作时,只有等待大约5秒才会更新模型。如果我之前单击按钮,则一切都会运行,直到更新模型

来自控制器

$scope.getPage = function(pageNum) {
  placesFact.getPage(pageNum).then(function(results){
    console.log('$scope.getPage fired', results); // This only fires if I wait about 5 seconds after previous run.
    $scope.details = results;
  });
};

来自工厂

 places.getPage = function(pageNum) {
  var promises = [];

  for(var i=0; i<pages[pageNum].length; i++) {
    promises.push(places.getPlace(pages[pageNum][i]));
  }
  console.log("places.getPage fired", promises); // This fired and changes the array
  return $q.all(promises);
};

places.getPlace = function(id) {
  var deferred = $q.defer(),
      request = {placeId: id};

  var map = new google.maps.Map(document.createElement('div'));

  var service = new google.maps.places.PlacesService(map);

  function callback(results, status){
    if(status === google.maps.places.PlacesServiceStatus.OK) {
      return deferred.resolve(results);
    }
  }
  service.getDetails(request, callback);
  return deferred.promise;
};

0 个答案:

没有答案