handle error callback on save() method $ngResource

守給你的承諾、 提交于 2019-12-23 20:57:20

问题


I need to handle error callback of an update operation, for this i'm using method save() like this:

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

Migparams service look like this:

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

This code works great but i need to know if an error occurs in database. I have searched in google but i didn't find this particular case. Is there a way of get this?. Thanks in advance


回答1:


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

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

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

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

Your code would look like:

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


来源:https://stackoverflow.com/questions/27405016/handle-error-callback-on-save-method-ngresource

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!