angularjs - 为什么 AngularJS $http success/error 方法被弃用?从 v1.6 中删除?

标签 angularjs angularjs-http angularjs-1.6

AngularJS 文档有一个针对 $http successerror 方法的弃用通知。这个抽象被从库中删除有什么具体原因吗?

最佳答案

问题在于 .success.error 方法不可链接,因为它们忽略返回值。这给熟悉链接的人带来了问题,并鼓励不熟悉链接的人编写糟糕的代码。见证 StackOverflow 上使用 deferred anti-pattern 的所有示例.

引用 AngularJS 团队一位成员的话:

IMO .success and .error were a bad bit of API design in the first place. This issue highlights a number of situations where developers get confused because they either expect .success and .error to work the same way as .then or vice versa. In a perfect world I would rather just ditch these $http specific "promises". Instead we could encourage developers to use the standard $q promise API .then and .catch. There is very little benefit IMO in working with explicit parameters over working with the response object.

— AngularJS Issue #10508 $http .success/.error dissimilar from how .then works.

Deprecation Notice (v1.5)

The $http legacy promise methods success and error have been deprecated. Use the standard then method instead. If $httpProvider.useLegacyPromiseExtensions is set to false then these methods will throw $http/legacy error.

— AngularJS $http Service API Reference -- deprecation notice

<小时/>

更新

已弃用的 .success.error 方法已从 AngularJS 1.6 中删除。

Due to b54a39, $http's deprecated custom callback methods - .success() and .error() - have been removed. You can use the standard .then()/.catch() promise methods instead, but note that the method signatures and return values are different.

$http(...)
  .then(function onSuccess(response) {
    // Handle success
    var data = response.data;
    var status = response.status;
    var statusText = response.statusText;
    var headers = response.headers;
    var config = response.config;
    ...
  }).catch(function onError(response) {
    // Handle error
    var data = response.data;
    var status = response.status;
    var statusText = response.statusText;
    var headers = response.headers;
    var config = response.config;
    ...
  });

— AngularJS Developer Guide - Migrating to v1.6 - http

关于angularjs - 为什么 AngularJS $http success/error 方法被弃用?从 v1.6 中删除?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47972937/

相关文章:

javascript - AngularJS 使用 ng-upload 上传图片

javascript - 自定义过滤器,用于从 AngularJS 1.x 中的一个选择框中删除另一个选择框中的选定选项

javascript - 如何通过绑定(bind)将模板传递给 AngularJS 组件

javascript - Angularjs jquery 状态之间的绑定(bind)

html - 将 Angular JS ui-bootstrap 下拉切换绑定(bind)到 ng-model

angularjs - AngularJS 中 $http.put 和 $http({method :'PUT' }) 的区别

angularjs - 在 Angularjs 应用程序中使用 $rootscope 的最佳实践?

angularjs - 如何使用 Typescript 实现过滤器?

javascript - 扁平化 AngularJS 中的嵌套 Promise

python - 无法在 Django 中获取 AngularJS $http.post 数据