javascript - AngularJS: Share factory between multiple modules -


let's have module called app injects 2 other modules called factories , controllers:

var app = angular.module("app", ["factroies", "controllers", "directives"]) .run(function ($rootscope, userfactory) {     userfactory.property = "somekickstartvalue"; }); 

the factories module holds factories:

var factories = angular.module("factories", []), factory = factories.factory("testfactory", {      property: "somevalue" }); 

and controllers module holds controllers:

var controllers = angular.module("controllers", ["factories"]), controller = controllers.controller("controller", function ($scope, testfactory) {     console.log(testfactory.property); // returns "some value" , not                                         // "somekickstartvalue" expected. }); 

the actual question:

why "somekickstartvalue" not apply controllers? far understand module app has it's own testfactory instance , module controllers has it's own well, there can't information shared between modules via factories. there way around, or have made mistake?

i fixed removing "factories" dependency of controller.

var controllers = angular.module("controllers", []), controller = controllers.controller("controller", function ($scope, testfactory) {     console.log(testfactory.property); // returns "somekickstartvalue" expected }); 

because not declare factories dependency, controllers module doesn't create it's own instance of factories , has access instance of app module injects controllers module.


Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -