简体   繁体   中英

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 ?

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.

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