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
loads correctly when the service returns a successful response.
If there is an error, the error message from the factory's log stating "Request Failed: blah blah" can be seen on the console.
The problem:
Despite an error occurring, I am not receiving the error message in the controller, and "gotcha" is not even being printed in the browser console. I need to retrieve error details in the controller so that I can display them in the view. I prefer not to pass $scope
in the factory.
What could be wrong with my Controller code?
I believe I am following a similar approach as outlined in AngularJS Failed Resource GET, but I am not achieving the desired outcome.