简体   繁体   中英

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM