AngularJS: How to handle success and error call backs with ngResource?

送分小仙女□ 提交于 2019-12-03 04:40:44
Gloopy

You can pass in a success a error callback using the following formats depending on how you are using the Resource (taken from the docs):

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

Your example is similar to the non-get "class" actions and would look something like this:

Transaction.delete({transactionId: $scope.transactions[index].uuid}, 
    function(successResult) {
        // do something on success
    }, function(errorResult) {
        // do something on error
        if(errorResult.status === 404) {            
        }
    }

Here is a related question regarding a failed GET resource.

Pass in the success and fail callback functions as arguments.

Transaction.delete({transactionId: $scope.transactions[index].uuid}, 
                   function(data) {
                      // success
                   }, function(e) {
                      // failure
                   });

From the docs you linked:

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

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