给出以下代码:

function Ctrl($scope, $http, $q) {
  var search = function(name) {
    if (name) {
      $http.get('http://api.discogs.com/database/search?type=artist&q='+ name +'&page=1&per_page=5').
        success(function(data3) {
          $scope.clicked = false;
          $scope.results = data3.results;
        });
    }
    $scope.reset = function () {
      $scope.sliding = false;
      $scope.name = undefined;
    };
  };
  $scope.$watch('name', search, true);

var done = $scope.getDetails = function (id) {
    $scope.clicked = true;
    $scope.sliding = true;
    var api = 'http://api.discogs.com/artists/';
    return $q.all([$http.get(api + id),
                  $http.get(api + id + '/releases?page=1&per_page=100')]);
};

done.then(function (){
 $scope.releases = data2.releases;
 $scope.artist = data;
 return $http.get('http://ws.audioscrobbler.com/2.0/?method=album.getinfo&api_key=e8aefa857fc74255570c1ee62b01cdba&artist=' + name + '&album='+ title +'&format=json');
});


我收到以下控制台错误:

TypeError: Object function (id) {
    $scope.clicked = true;
    $scope.sliding = true;
    var api = 'http://api.discogs.com/artists/';
    return $q.all([$http.get(api + id),
                  $http.get(api + id + '/releases?page=...<omitted>... } has no method 'then'
at new Ctrl (file:///C:/Users/Zuh/Desktop/AngularJS%20Discogs/js/services.js:27:9)


谁能指出我的错误可能在哪里?我正在执行getDetails之后定义.then ...

这是working Plunker

最佳答案

这是您更新的plunkr http://plnkr.co/edit/lTdnkRB1WfHqPusaJmg2?p=preview

angular.module('myApp', ['ngResource']);

  function Ctrl($scope, $http, $q) {
    var search = function(name) {
      if (name) {
        $http.get('http://api.discogs.com/database/search?type=artist&q='+ name +'&page=1&per_page=5').
          success(function(data3) {
            console.log(arguments)
            $scope.clicked = false;
            $scope.results = data3.results;
          });
      }
      $scope.reset = function () {
        $scope.sliding = false;
        $scope.name = undefined;
      };
    };
    $scope.$watch('name', search, true);

    var done = $scope.getDetails = function (id) {
        $scope.clicked = true;
        $scope.sliding = true;
        var api = 'http://api.discogs.com/artists/';
        var q =  $q.all([$http.get(api + id),
                      $http.get(api + id + '/releases?page=1&per_page=100')])
                  .then(function (ret){
                     //console.log(arguments)

                     $scope.releases = ret[1].data.releases;
                     $scope.artist = ret[0];
                     return $http.get('http://ws.audioscrobbler.com/2.0/?method=album.getinfo&api_key=e8aefa857fc74255570c1ee62b01cdba&artist=' + name + '&album='+ title +'&format=json');
                   })

        return q
      };

  }


总结修补程序:


将$ q.all()。then()部分移到done方法中
更注意then部分中处理程序收到的参数。

关于javascript - 在AngularJS中处理$ q.all和promises时出错,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22460972/

10-12 13:03