Nicolas Modrzyk
Nicolas Modrzyk

Reputation: 14197

Sharing resources between controllers in Angular.js

Simple question.

I have this resource:

var Company = $resource("/company/:_id", {_id: "@_id"}); 

That I want to share between different controllers.

Right now, I am copy-pasting things around but I am still not at the point where I want to add more code and use angular shared services

Any other option ?

Upvotes: 4

Views: 1915

Answers (1)

Umur Kontacı
Umur Kontacı

Reputation: 35478

Just put in a service or factory.

angular.module("myApp", []).
  factory("CompanyResource", function ($resource) {
    return $resource("/company/:_id", {_id: "@_id"}); 
  });

and then you can use it in the controller with

function MapCtrl($scope, $resource, $location, CompanyResource) {
   ...
   CompanyResource.query();
   ...
}

Note that you do not need the $ sign in front of the factory name.

Upvotes: 7

Related Questions