捕获Angular承诺两次成功响应

时间:2015-06-02 21:52:04

标签: angularjs

控制台输出似乎总是:

This is the first response
This is the second response

因为将首先调用内部方法的成功函数。这是正确的假设吗?订单有保证吗?

app.controller("ctrl", function($scope,$http) {

    var getUrl = function () {

          var config = {
              method: 'GET',
              url: 'some.txt'
          };


          return $http(config)
              .success(function (response, status, headers, config) {

                   console.log('This is the first response');

              })
              .error(function (data, status, headers, config) {
              });

     };   



    var init = function () {
        var promise = getUrl();
        promise.then(
           function() { 
              console.log('This is the second response');
           });
    };

    init();

 });

1 个答案:

答案 0 :(得分:0)

是的,因为$ http会自己返回一个承诺,而你上面所做的就是承诺链接。

尽管使用$ http.then(成功,错误).then(成功,错误)可能更好;