Angular.js delete resource with parameter

后端 未结 4 1666
醉酒成梦
醉酒成梦 2021-02-01 17:52

My rest api accpets DELETE requests to the following url

/api/users/{slug}

So by sending delete to a specified user (slug) the user would be de

4条回答
  •  情歌与酒
    2021-02-01 18:14

    params is an object of default request parameteres in your actions. If you want url parameters you have to specify them in the second parameter like this:

    angular.module('UserService',['ngResource']).factory('User', function($resource){
        var User = $resource('/api/users/:id1/:action/:id2', //add param to the url
        {id1:'@id'},
        { 
            delete_user: {
                method: 'DELETE'
            }
        }); 
    
        return User;
    }); 
    

    this works with either:

    // user has id
    user.$delete_user(function(){
      //success
    },function(){
      // error
    });
    

    or

    var data = {id:'id_from_data'};
    User.delete_user({},data);
    

    or

    var params = {id1:'id1_from_params'};
    User.delete_user(params);
    

    I've made a plnkr-example - you have to open your console to verify that the DELETE requests are correct.

    See parameterDefaults in the Angular resource documentation.

提交回复
热议问题