为什么不推荐使用 AngularJS $http success/error 方法?从 v1.6 中删除?
Why are AngularJS $http success/error methods deprecated? Removed from v1.6?
AngularJS 文档有一个针对 $http
success
和 error
方法的弃用通知。从库中删除此抽象是否有特定原因?
javascript 它使用的与 promises 相关的模式仅用于 .then(successCallback, errorCallback)
,所以他们可能打算使用 js 模式。
问题是 .success
和 .error
方法 不可链接 因为它们 忽略 return 值。这给熟悉 chaining 的人带来了问题,并鼓励不熟悉 chaining 的人编写糟糕的代码。看看 Whosebug 上所有使用 .
的例子
引用 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
更新
已从 AngularJS 1.6 中删除已弃用的 .success
和 .error
方法。
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 文档有一个针对 $http
success
和 error
方法的弃用通知。从库中删除此抽象是否有特定原因?
javascript 它使用的与 promises 相关的模式仅用于 .then(successCallback, errorCallback)
,所以他们可能打算使用 js 模式。
问题是 .success
和 .error
方法 不可链接 因为它们 忽略 return 值。这给熟悉 chaining 的人带来了问题,并鼓励不熟悉 chaining 的人编写糟糕的代码。看看 Whosebug 上所有使用
引用 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 methodssuccess
anderror
have been deprecated. Use the standardthen
method instead. If$httpProvider.useLegacyPromiseExtensions
is set tofalse
then these methods will throw$http/legacy
error.— AngularJS $http Service API Reference -- deprecation notice
更新
已从 AngularJS 1.6 中删除已弃用的 .success
和 .error
方法。
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; ... });