从$ http.success返回一个错误 [英] return an error from $http.success
问题描述
我在角工作,我需要进一步表示一个错误的承诺环比下跌时,其结果是在.success被解析()。
I am working in Angular and I need to signify an error to a promise further down the chain when the result is being resolved in .success().
我叫我的服务功能,从我的控制器像
I call a function in my service from my controller like
myService.myFunction().then(
function(results){
// do success stuff
},
function(err){
// do failure stuff
});
myFunction是类似
myFunction is something like
myFunction(){
return $http.get('url')
.success(function(results){})
.error(function(err){})
}
根据一定的条件下,我需要有。那么()执行errorCallback即使$ http.get()成功()被触发。我怎样才能使它看起来像$收到HTTP错误?
Based on certain conditions, I need to have .then() execute the errorCallback even though $http.get().success() was triggered. How can I make it look like $http received an error?
推荐答案
这是你需要做的实现,这将是使用一些修补程序的然后
而不是成功
在 $ HTTP
功能。
Some fixes that you need to do to achieve this would be use the then
instead of success
function on $http
.
而在然后
成功回调,你可以做一个收益$ q.reject(errordata子)
拒绝许环比下滑。
And in the then
success callback you can do a return $q.reject(errorData)
to reject the promise down the chain.
return $http.get('url').then(function(results){
if(condition) {
return $q.reject(errorData);
}
return result.data; //then callback get a object with properties
},function(error) {
return $q.reject(error);
})
成功
返回原始的 $ HTTP
的承诺,而然后
返回被成功和错误回调的返回值解决的承诺。
success
returns the original $http
promise whereas then
returns a promise that gets resolved with the return value of success and error callback.
这篇关于从$ http.success返回一个错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!