AngularJS - $resource different URL for Get and Post

前端 未结 5 1482
南笙
南笙 2020-12-24 14:35

$resource is awesome providing very convenient way to handle web services. What if GET and POST have to be performed on different URLs?

For example, GET URL is

5条回答
  •  南方客
    南方客 (楼主)
    2020-12-24 14:45

    If you add the hash with param names into the $resource call:

    $resource('localhost/pleaseGethere/:id', {id: '@id'});
    

    Then the :id will be mapped to id param when invoking the function (this will call GET localhost/pleaseGethere/123):

    Resource.get({id: 123});
    

    For POST, you simply don't assign the id param:

    Resource.post({}, {name: "Joe"});
    

    The proper URL will be called, which is in this case POST localhost/pleaseGethere (the trailing slash is stripped by ngResource).

    See http://docs.angularjs.org/api/ngResource.$resource -> Examples -> Credit card resource for more details.

提交回复
热议问题