angular $http/jquery 完全等效

编程入门 行业动态 更新时间:2024-10-26 10:28:08
本文介绍了angular $http/jquery 完全等效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

有没有办法用 angular $http 模块模拟 jquery 'complete' 回调?无论请求是成功还是失败,我都想执行一些代码,目前我发现自己必须编写以下代码:

Is there a way to emulate jquery 'complete' callback with angular $http module? I have some code I would like to execute no matter whether the request succeeded or failed and at the moment I find myself having to write this:

$http.get(someUrl).success(function(){
        successCode();
        completeCode();
    }).error(function(){
        errorCode();
        completeCode();
    })

但我宁愿写这样的东西:

but I would rather write something like:

$http.get(someUrl).success(function(){
        successCode();
    }).error(function(){
        errorCode();
    })plete(function(){
        completeCode();
    })

我也尝试过使用 promise API,但最终还是遇到了同样的问题.有什么建议吗?

I've tried also using the promise API but I end up having the same problem. Any suggestion?

推荐答案

2014 年 8 月更新: .always 已更名为 .finally最新版本的 Angular.更喜欢 .finally.always.

Update Aug 2014: .always has been renamed .finally in recent versions of Angular. Prefer .finally to .always.

请注意,为了支持 IE8,您必须使用括号表示法将其称为 ["finally"].

Note that in order to support IE8 you have to call it with bracket notation as ["finally"].

你可以在 AngularJS 中使用 .always

You can use .always in AngularJS

这个变化相当新(你可以在 jQuery 中做一段时间),你可以看到提交 此处.这要求您拥有 AngularJS 1.1.5 或更高版本.

This change is rather new (you could do that in jQuery for a while), you can see the commit here. This requires you to have AngularJS 1.1.5 or higher.

always(callback) – 允许您观察承诺的履行或拒绝,但这样做而不修改最终值.这对于释放资源或做一些事情很有用无论承诺被拒绝还是解决,都需要进行清理.请参阅完整规范更多信息.

always(callback) – allows you to observe either the fulfillment or rejection of a promise, but to do so without modifying the final value. This is useful to release resources or do some clean-up that needs to be done whether the promise was rejected or resolved. See the full specification for more information.

这篇关于angular $http/jquery 完全等效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-21 10:09:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1000508.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:angular   http   jquery

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!