0

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?

1 Answers1

1

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.

Chandermani
  • 42,589
  • 12
  • 85
  • 88
  • Thanks. Are there any other headers coming from $resource. With HTTP I thought there was something like .success(function (data, status, headers, config) Does the $resource allow me to define a .success and .error ? –  Oct 01 '13 at 09:27
  • The first callback is .success and the second callback is the .error in case of resource. Try to simulate an error, and see what is in the error object. – Chandermani Oct 01 '13 at 09:29
  • @Melina see my updated answer. The httpResponse object would have everything you are looking for. – Chandermani Oct 01 '13 at 09:30
  • Hello, I don't have any error to simulate but your answer is a big help. Do you think there's a way I can use .success and .error as that looks a lot cleaner to me. –  Oct 01 '13 at 09:32
  • This may help you, but not sure http://stackoverflow.com/questions/15531117/angularjs-1-1-3-resource-callback-error-and-success – Chandermani Oct 01 '13 at 09:40