I use the following pattern:
o in a service called resources I create resources as follows (assuming my webservces are unter the URL path /webapi):
angular.module('myApp')
.factory('resources', function ($resource) {
var baseUrl = location.pathname + 'webapi';
return {
baseUrl: baseUrl,
myServiceResource: $resource(baseUrl + '/my/service/:pathParam'),
myOtherServiceResource: $resource(baseUrl + '/my/other/service')
// etc.
};
});
o in a controller or another service you can then simply reference the resources service and use the globally defined resources (which are created only once):
angular.module('myApp')
.factory('aService', function (resources) {
// pathParam matches the parameter :pathParam in the URL
// param1 and param2 are passed as query parameters
var parameters = { pathParam: "pathValue", param1: "aValue", param2: "anotherValue" };
var response = resources.myServiceResource.get(parameters, function() {
// do something on success
doSomething(response.result);
}, function(httpResponse) {
// do something on error
});
});
successis deprecated in favor of the standardthen.$resource. It is wrapper over$httpthat supports the url format that you are looking for.