javascript 如何处理 AngularJS 中的工厂服务错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16973295/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to handle factory service errors in AngularJS
提问by Watt
Factory code
工厂代码
app.factory('abcFactory', function ($http, Config, $log) {
var serviceURL = Config.baseURL + '/results';
return{
results:function() {
var promise = $http({
method: 'GET',
url: serviceURL,
timeout: Config.httpTimeout
}).then(function(response) {
return response;
}, function(reason) {
$log.error("Request Failed: ", reason)
});
return promise;
}
}
});
Controller code
控制器代码
app.controller('abcController',function ($scope, $log,abcFactory, Config)
{
abcFactory.results(function(data){
$scope.results =data;
},function(response)
{
console.log("response"+response);
if(response.status === 0)
{
console.log("gotcha");
$scope.serviceTimedoutError(response.data, response.status, response.config);
} else
{
console.log("gotcha");
$scope.serviceFailure(response.data, response.status, response.config);
}
});
});
$scope.results
populates fine when service has returned good response.
$scope.results
当服务返回良好响应时填充良好。
And, when there is an error I can see on console the error message coming from factory code's log saying "Request Failed: blah blah "
.
而且,当出现错误时,我可以在控制台上看到来自工厂代码日志的错误消息说"Request Failed: blah blah "
。
My issue:When there is an error, why I am not seeing error message in controller, it is not even printing "gotcha"
in browser console. I need error details in controller, so that I can show it on view. I don't want to pass $scope
in factory.
我的问题:当出现错误时,为什么我在控制器中看不到错误消息,它甚至没有"gotcha"
在浏览器控制台中打印。我需要控制器中的错误详细信息,以便我可以在视图中显示它。我不想通过$scope
工厂。
What wrong I am doing in Controller code?
我在控制器代码中做错了什么?
If I am not wrong, I am doing something in line of AngularJS Failed Resource GETbut not getting desired result.
如果我没有错,我正在做一些与AngularJS Failed Resource GET 相关的事情, 但没有得到想要的结果。
回答by satchmorun
It looks like you want the results
function to take callbacks, but its arguments list is currently empty. You are returning the promis though.
看起来您希望该results
函数接受回调,但其参数列表目前为空。不过,您正在返回承诺。
So either change the call in the controller to:
因此,要么将控制器中的调用更改为:
abcFactory.results()
.success(function() { ... })
.error(function() { ... })
Or change the results function itself:
或者更改结果函数本身:
results: function(successCallback, errorCallback) {
var promise = $http(...).success(successCallback).error(errorCallback);
return promise;
}
回答by Michael
Deprecation NoticeThe $http legacy promise methods success and error have been deprecated. Use the standard then method instead. If $httpProvider.useLegacyPromiseExtensions is set to false then these methods will throw $http/legacy error.
弃用通知$http 遗留承诺方法成功和错误已被弃用。请改用标准 then 方法。如果 $httpProvider.useLegacyPromiseExtensions 设置为 false,那么这些方法将抛出 $http/legacy 错误。