角$ HTTP / jQuery的完全等同 [英] angular $http / jquery complete equivalent

查看:151
本文介绍了角$ HTTP / jQuery的完全等同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法模仿jQuery的'完整'具有角$ HTTP模块的回调?我有一些code,我想执行不管请求是成功还是失败,这时候我发现自己不得不写这篇文章:

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();
    }).complete(function(){
        completeCode();
    })

我试着用同样的承诺API,但我最终有同样的问题。任何建议?

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

推荐答案

更新2014年8月: 。总是已更名为。最后在最新版本的角。 preFER 。最后。总是

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

请注意,为了支持IE8你有括号来称呼它为 [终于]

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

您可以在AngularJS使用。总是

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) – 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.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆