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
Post a Comment