Resource sharing between controllers in Angular.js

Simple question.

I have this resource:

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

I want to share between different controllers.

Right now, I copied everything around, but I'm still not in the place where I want to add more code and use angular shared services

Any other option?

+4
source share
1 answer

Just supply 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.

+7
source

All Articles