user1943020
user1943020

Reputation:

How can I handle what happens when a call to $resource fails?

I have the following code:

        var entityResource = $resource('/api/Subject/GetSubjects')
        entityResource.query({ }, function (result) {
            $scope.grid.data = result;
            $scope.grid.backup = angular.copy(result);
            $scope.$broadcast('gridSetPristine');
            $scope.grid.fetching = false;
        }) 

This works but how can I add in more checks. How can I get any status codes received from HTML or handle if the call does not work?

Upvotes: 0

Views: 105

Answers (1)

Chandermani
Chandermani

Reputation: 42669

There is a second callback available on all methods on resource, which is injected with the error object

entityResource.query({ }, function (result) {
            $scope.grid.data = result;
            $scope.grid.backup = angular.copy(result);
            $scope.$broadcast('gridSetPristine');
            $scope.grid.fetching = false;
        },
        function(error){
            //Probe error object here.
        }); 

As per documentation

Success callback is called with (value, responseHeaders) arguments. Error callback is called with (httpResponse) argument.

This error callback is invoked i believe for http responses which are not in 2xx range.

Upvotes: 1

Related Questions