假设我有一个名为App的模块,它注入了两个其他模块,分别是工厂和控制器:
var app = angular.module("app", ["factories", "controllers", "directives"])
.run(function ($rootScope, userFactory) {
userFactory.property = "someKickstartValue";
});
“factories(工厂)”模块包含所有的工厂:
var factories = angular.module("factories", []),
factory = factories.factory("testFactory", {
property: "someValue"
});
控制器模块包含所有的控制器:
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.
});
实际问题:
为什么“someKickstartValue”不适用于控制器?据我所了解,模块app有它自己的testFactory实例,而模块controllers也有它自己的实例,因此不能通过工厂在模块之间共享信息。还有其他解决方法吗?或者是我的错误?