return 来自 $http.success 的错误
return an error from $http.success
我在 Angular 工作,当在 .success() 中解决结果时,我需要向链中更下游的承诺表示错误。
我从我的控制器调用服务中的一个函数,例如
myService.myFunction().then(
function(results){
// do success stuff
},
function(err){
// do failure stuff
});
myFunction 类似于
myFunction(){
return $http.get('url')
.success(function(results){})
.error(function(err){})
}
基于某些条件,即使触发了 $http.get().success(),我也需要让 .then() 执行 errorCallback。我怎样才能让它看起来像 $http 收到错误?
您需要做一些修复来实现此目的,即在 $http
上使用 then
而不是 success
函数。
并且在 then
成功回调中,您可以执行 return $q.reject(errorData)
拒绝链下的承诺。
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);
})
success
return 是最初的 $http
承诺,而 then
return 是一个以 return 成功值解决的承诺和错误回调。
我在 Angular 工作,当在 .success() 中解决结果时,我需要向链中更下游的承诺表示错误。
我从我的控制器调用服务中的一个函数,例如
myService.myFunction().then(
function(results){
// do success stuff
},
function(err){
// do failure stuff
});
myFunction 类似于
myFunction(){
return $http.get('url')
.success(function(results){})
.error(function(err){})
}
基于某些条件,即使触发了 $http.get().success(),我也需要让 .then() 执行 errorCallback。我怎样才能让它看起来像 $http 收到错误?
您需要做一些修复来实现此目的,即在 $http
上使用 then
而不是 success
函数。
并且在 then
成功回调中,您可以执行 return $q.reject(errorData)
拒绝链下的承诺。
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);
})
success
return 是最初的 $http
承诺,而 then
return 是一个以 return 成功值解决的承诺和错误回调。