简体   繁体   中英

Optional url parameters in AngularJs Resource

I have this kind of resource: var Products = $resource('companies/:companyId/products') The problem is, that I would like to get the products of all companies by url companies/products , but using resource and not providing companyId I am getting companies//products . Probably I could use another url, like just /products , but does it mean that I have to have another resource for the same thing?

In this simple case I could change the url to companies/products/:companyId , but it seems to be quite general case.

Yes, currently you need to define another $resource.

But you can wrap multiple instances of $resource into a single service if you want...

app.factory('mergedRes', function($resource) {
  var r1 = $resource('/companies/:companyId/products');
      r2 = $resource('/companies/products');

  r1.getAll = r2.query.bind(r2);

  return r1;
});

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