问题描述
AngularJS文档已针对 $ http
成功
和错误
方法。
The AngularJS documentation has a Deprecation Notice for the $http
success
and error
methods. Is there a specific reason this abstraction was removed from the library?
推荐答案
问题是 .success是否有特定原因?
和 .error
方法不可链接,因为它们忽略返回值。这给熟悉 chaining 的人带来了问题,并鼓励了不熟悉 chaining 的人提供的劣质代码。见证所有使用的示例。
The problem was that .success
and .error
methods are not chainable because they ignore return values. This caused problems for people familiar with chaining and encouraged poor code from people unfamiliar with chaining. Witness all the examples on StackOverflow that use the deferred anti-pattern.
要引用AngularJS团队之一:
To quote one of the AngularJS team:
— AngularJS Issue #10508 $http
.success/.error
dissimilar from how .then
works.
UPDATE
已弃用的。成功
和 .error
方法已从AngularJS 1.6中删除。
UPDATE
The deprecated .success
and .error
methods have been removed from AngularJS 1.6.
$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 $ http成功/错误方法?从v1.6中删除了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!