tomwyn tomwyn - 27 days ago 13
AngularJS Question

How to pass data from controller to service?

I have an angular service with a function:

//service.js

function DoSomething ($resource) {
var resourceUrl = 'right/here';

return $resource(resourceUrl, {}, {
'action': {
method: 'POST'
}
});
}


and a @RestController with 2 parameters (param1, param2).
When I use the service like this:

//service.js
function DoSomething ($resource) {
var resourceUrl = 'right/here';

return $resource(resourceUrl, {param1:"abc", param2:"def"}, {
'action': {
method: 'POST'
}
});


it works fine. But when I want to pass these values from my controller, like this:

//controller.js

DoSomething.action({param1:"abc"},{param2:"def"});


while service part remains empty

//service.js

return $resource(resourceUrl, {}, {


my @RestController receives only one value - the first from the left. How to solve this?

Answer

With $resource(resourceUrl, {}, {, you can pass parameters from your controllwe as below,

//controller.js

DoSomething.action({}, {param1: "abc", param2: "def"});