.success之间()。然后角$ HTTP差异和() [英] Angular $http difference between .success() and .then()

查看:109
本文介绍了.success之间()。然后角$ HTTP差异和()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时我有麻烦获取使用$ http服务我的数据。可我没有100%的理解.success()和。然后之间的主要区别()

以下是我可以获取使用$ HTTP()的数据。则()的例子,但使用$我无法获取我的数据HTTP()成功()。任何人都可以解释为什么?

使用

code $ http.success() http://jsfiddle.net/xoonpLte/11/

  VAR manuRep = angular.module('manuRep',['ui.bootstrap']);
  manuRep.controller('MyAppController',函数($范围,$ HTTP){
    $ http.get('https://dl.dropboxusercontent.com/u/59954187/jobs.json')。
      成功(功能(响应){
        $ scope.cats = response.data;
      })。
      错误(功能(错误){
        警报(JSON.stringify(错误));
      返回错误;
      });
  });

使用

code $ http.then() http://jsfiddle.net/xoonpLte/12/

  VAR manuRep = angular.module('manuRep',['ui.bootstrap']);
  manuRep.controller('MyAppController',函数($范围,$ HTTP){
    $ http.get('https://dl.dropboxusercontent.com/u/59954187/jobs.json')。
      然后(功能(响应){
        $ scope.cats = response.data;
      });
  });


解决方案

。然后是一个承诺

.success .error 是一个回调。

为什么回调或承诺?

1。承诺能够处理全球性的错误。结果
2。承诺可以被链接(你没有这种封装喜欢回调)

P =承诺,结果
R =响应,结果
E =错误。

1

  P1(R1)
.P2(R 2)
.P3(R3,E3)

如果在承诺1,2或3。结果,错误错误3将被称为
这是不可能的回调。

2 结果
如果你有3个承诺:

  P1(R1,E1)
.P2(R 2,E 2)
.P3(R3,E3)

如果你有3个回调

  P1()成功(P2()。成功(P3()成功()。错误())。错误())。错误()

修改

 。服务('验证',函数(){
  this.isLoggedIn =功能(){
     返回$ http.get(URL / isLoggedIn');
  };
})。服务('的getData',函数(){
  this.name =功能(){
    返回$ http.get(URL /数据/名称');
  }
}).controller('MyCTRL',['验证','的getData',函数(AUTH,的getData){   Auth.isLoggedIn()。然后(功能(RESP){
     返回getData.name(); //通过发送值回在矿井响应,接下来的反应叫做
   })
   。然后(功能(RES){
        //我们到了
    },功能(错误){
        //环球错误。
        //这将在P1或P2被称为
    })
}]);

Sometimes I have trouble to fetch my data using $http service. And I did not 100% understand the main difference between .success() and .then()

The following is an example that I can fetch the data using $http().then(), but I can't fetch my data using $http().success(). Can anyone explain why?

Code using $http.success() http://jsfiddle.net/xoonpLte/11/

var manuRep = angular.module('manuRep', ['ui.bootstrap']);
  manuRep.controller('MyAppController', function($scope, $http) {
    $http.get('https://dl.dropboxusercontent.com/u/59954187/jobs.json').
      success(function(response) {
        $scope.cats = response.data;    
      }).
      error(function(error){
        alert(JSON.stringify(error));
      return error;
      });
  });

Code using $http.then() http://jsfiddle.net/xoonpLte/12/

var manuRep = angular.module('manuRep', ['ui.bootstrap']);
  manuRep.controller('MyAppController', function($scope, $http) {
    $http.get('https://dl.dropboxusercontent.com/u/59954187/jobs.json').
      then(function(response) {
        $scope.cats = response.data;    
      });
  });

解决方案

.then is a promises

.success and .error is a callback.

Why callbacks or Promises?

1 . Promises can handle global errors.
2 . Promises can be chained (you don't have this encapsulation like callbacks)

P = promise,
R = response,
E = error.

1:

P1(R1)
.P2(R2)
.P3(R3, E3)  

Error 3 will be called if there is an error in promise 1, 2 or 3.
That is not possible with callbacks.

2:
If you have 3 promises:

P1(R1, E1)
.P2(R2, E2)
.P3(R3,E3)

If you have 3 callbacks

P1().success(P2().success(P3().success().error()).error()).error()

EDIT

.service('Auth', function(){
  this.isLoggedIn = function(){
     return $http.get('url/isLoggedIn');
  };
})

.service('getData', function(){
  this.name = function(){
    return $http.get('url/data/name');
  }
})

.controller('MyCTRL', ['Auth', 'getData', function(auth, getData){

   Auth.isLoggedIn().then(function(resp){
     return getData.name(); //By sending a value back inthe response, next response is called
   })
   .then(function(res){
        //Here we are
    }, function(err){
        //Global error.
        //This will be called in P1 or P2
    })


}]);

这篇关于.success之间()。然后角$ HTTP差异和()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆