AngularJS则()的行为比成功不同() - 错误() [英] AngularJS then() behaves differently than success()-error()

查看:160
本文介绍了AngularJS则()的行为比成功不同() - 错误()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于成功()错误()函数去$ P $在AngularJS pcated,我更新我的codeS,用替换它们则()。现在据我了解,这两个件codeS应该表现相同的:

As the success() and error() functions are deprecated in AngularJS, I am updating my codes, replacing them with then(). Now according to my understanding, these two pieces of codes should behave identically:

$http
   .get(/* some params */)
   .success(function() {
      // success cases here
   })
   .error(function() {
      // error cases here
   });

$http
   .get(/* some params */)
   .then(function() {
      // success cases here
   }, function() {
      // error cases here
   });

但在某些情况下,我得到不同的行为。能否请您给我解释一下为什么会发生,更重要的是,这将是使用,以保证相同的行为方式则()功能?

推荐答案

.success .error 方法忽略返回值。结果
因此他们的不适合链接

var httpPromise = $http
       .get(/* some params */)
       .success(function onSuccess(data, status, headers, config) {
          var modifiedData = doModify(data);
          //return value ignored
          return modifiedData;
       })
       .error(function onError(data, status, headers, config) {
          // error cases here
       });

httpPromise.then(function onFullfilled(response) {
    //chained data lost
    //instead there is a response object
    console.log(response.data); //original data
    console.log(response.status); //original status
});

在otherhand,在。然后 .catch 方法返回的衍生承诺 适用于链接从返回(或抛出)值或从一个新的承诺。

On the otherhand, the .then and .catch methods return a derived promise suitable for chaining from returned (or throw) values or from a new promise.

var derivedPromise = $http
       .get(/* some params */)
       .then(function onFulfilled(response) {
          console.log(response.data); //original data
          console.log(response.status); //original status

          var modifiedData = doModify(response.data);
          //return a value for chaining
          return modifiedData;
       })
       .catch(function onRejected(response) {
          // error cases here
       });

derivedPromise.then(function onFullfilled(modifiedData) {
    //data from chaining
    console.log(modifiedData);
});

响应对象VS四个参数

另外请注意, $ HTTP 服务提供的四个参数 (数据,状态,头,配置)当调用提供给函数的 .success .error 方法。

Response Object vs Four Arguments

Also notice that the $http service provides four arguments (data, status, headers, config) when it invokes the function provided to the .success and .error methods.

$ Q 服务只提供一个参数(响应),以提供给。然后 .catch 方法。在由 $ HTTP 服务创建承诺的情况下,响应对象具有以下属性: 1

The $q service only provides one argument (response) to the functions provided to the .then or .catch methods. In the case of promises created by the $http service the response object has these properties:1


  • 数据 - {字符串|对象} - 与变换函数变换的响应体

  • 状态 - {数} - 响应的HTTP状态code

  • 标题 - {功能([headerName])} - 头getter函数

  • 配置 - {}对象 - 这是用来生成请求的配置对象

  • 状态文本 - {string}里 - 响应的HTTP状态文字

由于调用一个承诺,那么方法返回一个新派生的承诺,它很容易可以创建诺言链。它可以创建任意长度的链,因为承诺可以与另一个承诺(这将进一步推迟其分辨率)来解决,有可能暂停/在链中的任何一点推迟的承诺分辨率。这使得它可以实现强大的API。 2

Chaining promises

Because calling the then method of a promise returns a new derived promise, it is easily possible to create a chain of promises. It is possible to create chains of any length and since a promise can be resolved with another promise (which will defer its resolution further), it is possible to pause/defer resolution of the promises at any point in the chain. This makes it possible to implement powerful APIs.2

这篇关于AngularJS则()的行为比成功不同() - 错误()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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