AngularJS 文档有一个针对 $http
success
和 error
方法的弃用通知。这个抽象被从库中删除有什么具体原因吗?
最佳答案
问题在于 .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 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
更新
已弃用的 .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 - 为什么 AngularJS $http success/error 方法被弃用?从 v1.6 中删除?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47972937/