Sharing resources between controllers in Angular.js

后端 未结 1 866
后悔当初
后悔当初 2021-01-13 14:10

Simple question.

I have this resource:

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

That I want to share between d

相关标签:
1条回答
  • 2021-01-13 15:04

    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.

    0 讨论(0)
提交回复
热议问题