简体   繁体   English

无法理解AngularJS Promises

[英]Trouble understanding AngularJS Promises

I'm currently learning AngularJS and I'm having trouble translating a piece of code from an example to my own code. 我目前正在学习AngularJS,我在将一段代码从一个示例转换为我自己的代码时遇到了麻烦。

In the following example from Urish , I understand that the variable promise must contain the functions and queries necessary for the rest of the function to execute. Urish的以下示例中,我了解变量promise必须包含执行其余函数所需的函数和查询。

var promise = asyncFunction(parameters); 

promise.then( 
function (result) { 
 // Do Something with the result 
}, 
function (error) { 
 // Handle error (exception, etc). 
}); 

So in my case, if I want to first make two $http queries and with the data received populate the url of a third query, would it be something like this? 所以在我的情况下,如果我想首先进行两个$ http查询并且收到的数据填充第三个查询的url,它会是这样的吗?

var promise = $scope.getDetails(id); 

  $scope.getDetails = function (id) {
    $http.get('http://api.discogs.com/artists/' + id).
      success(function(data) {
          $scope.artist = data;
      });
    $http.get('http://api.discogs.com/artists/' + id + '/releases?page=1&per_page=100').
      success(function(data2) {
          $scope.releases = data2.releases;
      });
    $scope.clicked = true;
    $scope.sliding = true;
  } 

  promise.then( 
    $scope.getImages = function (title, name) {
        $http.get('http://ws.audioscrobbler.com/2.0/?method=album.getinfo&api_key=e8aefa857fc74255570c1ee62b01cdba&artist=' + name + '&album='+ title +'&format=json').
          success(function(data4) {
              $scope.images = data4;
          });
    },
  function (error) { 
   // Handle error (exception, etc). 
  }); 

Is this the right approach? 这是正确的方法吗? Am I missing something? 我错过了什么吗?

EDIT : I've created a working Plunker with Benjamins solution, but the AngularJS brakes. 编辑 :我已经创建了一个使用Benjamins解决方案的Plunker ,但是AngularJS制动器。 Any idea where the issue might be? 知道问题可能在哪里?

In order to combine promises, like Florian suggested you should use .all : 为了结合承诺,像弗洛里安建议你应该使用.all

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

Then usage is: 那么用法是:

$scope.getDetails(YOURID).then(function(results){
    $scope.artist = results[0];
    $scope.releases = results[1].releases;
});

Or in the three queries example: 或者在三个查询示例中:

$scope.getDetails(YOURID).then(function(results){
    $scope.artist = results[0];
    $scope.releases = results[1].releases;
    // data for getImages available here
    return $http.get('http://ws.audioscrobbler.com/2.0/?...' + results[0]...
});

(Also, I'd put it in a service) (另外,我把它放在服务中)

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

You must accept $q as a dependency for the controller. 您必须接受$ q作为控制器的依赖项。 $q.all() takes multiple promises and returns a promise which resolves or gets rejected depending upon the passed in promises. $ q.all()接受多个promises并返回一个promise,它根据传入的promises来解析或被拒绝。 Read about $q at http://docs.angularjs.org/api/ng/service/$q You can do 在http://docs.angularjs.org/api/ng/service/$q上阅读有关$ q的信息。你可以这样做

var promise = $scope.getDetails(id);
promise.then(...,...);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM