AngularJS: Share factory between multiple modules
angularjs, javascript
Solution
I fixed it by removing the "factories" dependency of the controller.
var controllers = angular.module("controllers", []),
controller = controllers.controller("controller", function ($scope, testFactory) {
console.log(testFactory.property); // Returns "someKickstartValue" as expected
});
Because I now do not declare factories as dependency, the controllers module doesn't create it's own instance of factories and has access to the instance of the app module which injects the controllers module.
Problem
Let's say I have a module called App which injects two other modules called factories and controllers: ``` var app = angular.module("app", ["factories", "controllers", "directives"]) .run(function ($rootScope, userFactory) { userFactory.property = "someKickstartValue"; }); ``` The factories module holds all factories: ``` var factories = angular.module("factories", []), factory = factories.factory("testFactory", { property: "someValue" }); ``` And the controllers module holds all controllers: ``` var controllers = angular.module("controllers", ["factories"]), controller = controllers.controller("controller", function ($scope, testFactory) { console.log(testFactory.property); // Returns "Some Value" and not // "someKickstartValue" as expected. }); ``` The actual Question: Why does the "someKickstartValue" not apply to the controllers? As far as I do understand the module app has it's own testFactory instance and the module controllers has it's own as well, so there can't be any information shared between modules via factories. Is there a way around, or have I made a mistake?