11

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.

1 Answer 1

13

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;
});
Sign up to request clarification or add additional context in comments.

2 Comments

what if you want to chain 3 difference resources, how would that look like?
I have tried this solution but it is still showing r1's /companies//products even if companyId is empty.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.