声明中angularJS承诺名为成功/错误回调 [英] declaring a promise in angularJS with named success/error callbacks
本文介绍了声明中angularJS承诺名为成功/错误回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图做的非常类似$ http服务的东西。从我的理解$ HTTP返回一个承诺的对象。
I am trying to do something very similar to the $http service. From my understanding $http return a promise object.
在使用它的语法是:
$http(...).success(function(data)) {
//success callback
}).error(function(data)) {
//error callback
})
我想这样做是一样的,但考虑我的API是GetUserProfile,所以我希望有语法:
I would like to do just the same but consider my API is GetUserProfile, so I wish to have the syntax :
GetUserProfile(...).success(function(data) {
// success callback
}).error(function(data)) {
// error callback
})
我怎么能做到,使用一个承诺?
how can I accomplish that using a promise ?
推荐答案
与开放源代码的好处是,你可以阅读源代码。这里的$ http服务是如何做的:
The nice thing with open-source is that you can read the source. Here's how the $http service does it:
promise.success = function(fn) {
promise.then(function(response) {
fn(response.data, response.status, response.headers, config);
});
return promise;
};
promise.error = function(fn) {
promise.then(null, function(response) {
fn(response.data, response.status, response.headers, config);
});
return promise;
};
这篇关于声明中angularJS承诺名为成功/错误回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文