如何在服务和控制器中使用angularjs承诺链?

如何在服务和控制器中使用angularjs承诺链?,angularjs,angular-promise,Angularjs,Angular Promise,我在web上找到了这个plnkr链接,但我需要将它用于2个或3个以上的ajax调用,这不需要来自第一个ajax调用的参数。如何处理错误 var app = angular.module("app", []); app.service("githubService", function($http, $q) { var deferred = $q.defer(); this.getAccount = function() { return $http.get('https:/

我在web上找到了这个plnkr链接,但我需要将它用于2个或3个以上的ajax调用,这不需要来自第一个ajax调用的参数。如何处理错误

var app = angular.module("app", []);

app.service("githubService", function($http, $q) {

  var deferred = $q.defer();

  this.getAccount = function() {
    return $http.get('https://api.github.com/users/haroldrv')
      .then(function(response) {
        // promise is fulfilled
        deferred.resolve(response.data);
        return deferred.promise;
      }, function(response) {
        // the following line rejects the promise 
        deferred.reject(response);
        return deferred.promise;
      });
  };
});

app.controller("promiseController", function($scope, $q, githubService) {

  githubService.getAccount()
    .then(
      function(result) {
        // promise was fullfilled (regardless of outcome)
        // checks for information will be peformed here
        $scope.account = result;
      },
      function(error) {
        // handle errors here
        console.log(error.statusText);
      }
    );
});

您可以使用
$q.all

var promises=[
$http.get(URL1),
$http.get(URL2),
$http.get(URL3),
$http.get(URL4)
];

$q.all(promises).then(function(response){
console.log('Response of Url1', response[0]);
console.log('Response of Url2', response[1]);
console.log('Response of Url3', response[2]);
console.log('Response of Url4', response[3]);
}, function(error){

});

我已经用
$q

$q.all
用于本例。它将同时调用
getAccount
getSomeThing
api

var app = angular.module("app", []);

app.service("githubService", function($http, $q) {

  return {

    getAccount: function () {
      return $http.get('https://api.github.com/users/haroldrv');
    },

    getSomeThing: function () {
      return $http.get('some thing url');
    }
  };

});

app.controller("promiseController", function($scope, $q, githubService) {

  function initData () {
    $q.all([githubService.getAccount(), githubService.getSomeThing()])
      .then(
        function (data) {
          $scope.account = data[0];
          $scope.someThing = data[1];
        },
        function (error) {

        }
      );
  }
});

首先,对于每个要返回承诺的ajax调用,应该将
deferred
变量设置为本地变量。因此,您必须创建2-3个函数(与ajax调用的数量相同),并将它们保存在一个数组中。那么你应该使用:

$q.all([ajax1,ajax2,ajax3]).then(function(values){
    console.log(values[0]); // value ajax1
    console.log(values[1]); // value ajax2
    console.log(values[2]);}); //value ajax3
例如:

function ajax_N() {
   var deferred = $q.defer();

    http(...).then((response) => {
      deferred.resolve(response);
   }, (error) => {
      deferred.reject(error);
   });

   return deferred.promise;
}

$q.all([
        ajax_1,ajax_2,ajax_3
     ]).then(function(values) {        
          console.log(values); 
          return values;
        });

给我们提供更多关于不同ajax调用的详细信息我们的意思是更多,不是不同,我现在已经解决了这个问题。你可以使用
$q.all
等待所有承诺得到解决,就像
$q.all([promise1,promise2])。然后(功能(数据){//success})
这个代码也使用了反模式,
$http
返回一个承诺,因此没有理由将其包装在一个
延迟的
@PankajParkar是正确的,诚实地说,
$q是一个快速的google。所有的例子都给出了你需要的(google上的第一个结果:)