问题
Using AngularJS 1.2.16 and angular-resource 1.2.16.
I have a resource like:
$resource('api/:variable/path',
{
variable:'@variableName'
});
When I do a get using something like
resourceIns.get({variable:'taco'});
The resulting ajax call replaces :variable properly and i get
api/taco/path
If I do a post like
resourceIns.save({variable:'taco'});
the resulting ajax call looks like
api/path
and 'taco' gets put in the POST body...
I've had trouble finding others complaining about this so, maybe this is what's supposed to happen?
edit: I just discovered that get uses 'variable' and save/POST uses 'variableName' in the above example. Anybody have an explanation for that?
Here's a fiddle showing the situation: fiddle
回答1:
I ran across the same issue or one that presented itself in the same fashion. My resource, too, was not respecting parameters passed in through a .post method.
I was able to get it to work by directly passing in the expected parameters.
Using a $resource:
angular.module('myApp')
.factory('ModuleProductProducts', function ($resource) {
return $resource('/module-api/product-products/:siteId/:id/:controller', {
id: '@id'
},
{
'updateMedia': {
method: 'POST',
url: 'module-api/product-products/:id/media/:mediaId',
// *** Here ***
params: {
id: '@id',
mediaId: '@mediaId'
}
// ************
}
});
});
来源:https://stackoverflow.com/questions/24025749/angularjs-resource-not-replacing-variable-in-url-template-for-post