Please see below the structure of my angular application:
I have a page called 'firm.html' which contains a button. Clicking this button executes the code below.
Controller
The controller calls a Service function. The generationInProgress
variable is used in an ng-show to toggle the visibility of a loading gif on the HTML page
$scope.generationInProgress = true;
firmService.processFirm(firmRequest).then(function(response) {
window.location.href = "firm/process";
$scope.generationInProgress = false;
});
Firm Service
This is a service that handles the Firm operations with the following function called above
this.processFirm = function(firmRequest) {
return httpService.put('firm/process', firmRequest);
};
HTTP Service
This is a service that handles all calls to the service. It is used by multiple services, including the firmService above. Here is the put
method as called above
this.put = function(url, data) {
return promise = $http.post(url, data).success(function(response) {
return response;
}).error(function(response) {
console.log("error");
});
};
If a HTTP error code is returned by the server, obviously the .error
function is executed. If I had a dedicated error page, I could just redirect to that page.
However, I need to display the error on the 'firm.html' page while also setting the $scope.generationInProgress
back to false so that the loading gif is no longer displayed. None of the code that does this can be located within the httpService because it is a common service used by many different components.
I am unsure how to propagate the error back to the controller in order to accomplish this. Do I just put return response;
in both the .success
and .error
and use an IF statement in the controller to test for the HTTP code? Is there an alternative method?
Any advice is appreciated.