Handle error callback in save () method $ ngResource

I need to handle the error callback of the update operation, for that I use the method save()

as follows:

$scope.save = function (params) {  
  MigParams.save(params);
};

      

Migparams

the service looks like this:

angular.module('monitor').
    factory('MigParams', function ($resource) {        
        return $resource('/restful/migparams');    
});

      

This code works great, but I need to know if there was an error in the database. I have searched on google but I have not found this specific case. Is there a way to get this ?. thanks in advance

+3


source to share


1 answer


From https://docs.angularjs.org/api/ngResource/service/$resource :

non-class actions GET: Resource.action ([parameters], postData, [success], [error])

non-GET class instance: Resource.action ([parameters], [success], [error])

$resource

the save method falls into the "non-GET" class instance category), so its error callback is the third argument.



Your code will look like this:

$scope.save = function (params) {  
  MigParams.save(params, 
    function(resp, headers){
      //success callback
      console.log(resp);
    },
    function(err){
      // error callback
      console.log(err);
    });
};

      

+10


source







All Articles