ngresource - How send some params by POST and some by GET in $ng-resource in Angularjs -
we have service using ng-resource:
var app = angular.module('salonesbelleza', []); app.service("centroborrar_srv", function ($resource, urlbaseapi_srv) { return{ return $resource('wa-centros/:id', {id:'@id', access_token:'@access_token'}, { "post": { method: "delete", isarray: false, headers: { 'accept': 'application/json; q=1.0, */*; q=0.1', 'content-type':'application/json' } }} ); }
});
in controller use way
var centroborrar_data = centroborrar_srv.post({ id:10, othervar1:'value_1', othervar1:'value_2', access_token:'my token' });
with ng-resource delete element id=10 using vars acces_token
this work well. , url generated ng-resource
wa-centros/10?access_token=my_token&othervar1=value_1&othervar2=value_2
all vars sent using in url want send vars using post , others using get. example, want send othervar1 , othervar2 using post.
is there way can force in ng-resource definition var in controller must sent post , must sent using get
i feel lost on subject
thank in advance
yes, possible add parameters. have declare custom action this. there 2 ways in custom action "non-get" type can created ( taken angular documentation ) :
- non-get "class" actions: resource.action([parameters], postdata, [success], [error])
- non-get instance actions: instance.$action([parameters], [success], [error])
as see using first, namely non-get "class" action. based on example should have :
return $resource('rest-link/:id', {id:'@id', access_token:'@access_token'}, { "yourcustomactionnamehere": { method: "post", isarray: false, headers: { 'accept': 'application/json; q=1.0, */*; q=0.1', 'content-type':'application/json' }, params : {yourparam1 : val} }} );
notice params have been added resource action, namely yourparam1. mentioned before non-get "class" action, meaning pass data object too. should take consideration when calling resource :
<yourresourcenamehere>.yourcustomactionnamehere({yourparam1: 12}, dataobject)
you should take care parameters, namely default parameters. in case of non-get method yours not providing value parameter paramname
, means search go on object properties defined in dataobject
sent post method, searching name-value
pair name
same paramname
.
you should have on instance methods, easier use in opinion , provide greater flexibility , readability. example have call centroborrar.$save({param : val})
post action, centroborrar instance , hold data object. ( short example here ). hope helps.
Comments
Post a Comment