Angular.js删除与参数资源 [英] Angular.js delete resource with parameter
问题描述
我的REST API accpets DELETE请求到以下网址
My rest api accpets DELETE requests to the following url
/api/users/{slug}
因此,通过发送删除对指定的用户(塞),用户将被删除。这里是服务code:
So by sending delete to a specified user (slug) the user would be deleted. here is the service code:
angular.module('UserService',['ngResource']).factory('User', function($resource){
var User = $resource('/api/users/:id1/:action/:id2', //add param to the url
{},
{
delete_user: {
method: 'DELETE',
params: {
id1:"@id"
}
},
update: {
method: 'PUT',
params: {
id1:"@id"
}
}
});
return User;
});
我通过打电话
user.$delete_user({id:user.id}, function(){}, function(response){});
但请求似乎发送到错误的URL。
However the request seems to be send to the wrong url.
/api/users?id=4
所以参数实际上是缺少,结果我得到不允许405方法。是否有机会在我的API的风格发送删除请求?
So the parameter is actually missing, as a result I get a 405 Method not allowed. Is there any chance to send the delete request in the style of my api?
推荐答案
PARAMS
是在你的行动默认请求parameteres的对象。如果你想要的URL参数你有这样的第二个参数中指定它们:
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;
});
这适用于两种:
// user has id
user.$delete_user(function(){
//success
},function(){
// error
});
或
var data = {id:'id_from_data'};
User.delete_user({},data);
或
var params = {id1:'id1_from_params'};
User.delete_user(params);
我做了一个 plnkr,例如 - 你有打开您的控制台,以验证该DELETE请求是正确的。
I've made a plnkr-example - you have to open your console to verify that the DELETE requests are correct.
请参阅 parameterDefaults 的在角资源文档。
这篇关于Angular.js删除与参数资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!