nickponline
nickponline

Reputation: 25914

How do I pass parameter to an angular service?

I have made simple HTTP based API and I would like to make POST calls to it like:

http://mysite.com/api/v1/person/something
http://mysite.com/api/v1/person/else
http://mysite.com/api/v1/person/someword

in general

http://mysite.com/api/v1/person/<word>

In angular I have created a service

angular.module('personService', ['ngResource']).
    factory('Person', function($resource){
      return $resource('/api/v1/person/:word', {}, {
        action:     {method:'{POST',  params:{ ???? },          isArray : false},
      });
  });

And in my controller which uses the service I would like to be able to call Person.action() and pass it a parameter to determine what word is, for example:

Person.action('something', function(d) { 
 console.log('Result of api/v1/person/something')
}); 

but I'm not sure how to connect the two. See ???? in the third code block.

Upvotes: 2

Views: 2982

Answers (1)

zs2020
zs2020

Reputation: 54514

Try this:

params:{ word: '@inputWord' }

Person.action({inputWord: 'somethings'}, function(d) { 
    console.log('Result of api/v1/person/something')
});

word matches the :word variable in the url and the passed in object's key inputWord matches @inputWord.

Upvotes: 1

Related Questions