如何将 $rootscope
注入(inject) wenzey.services,以便可以在我的应用程序中访问它?
(function () {
'use strict';
/**
*
*/
angular
.module('wenzey.services', '$rootScope', [])
$rootScope.type = "consumer";
})();
这是我当前收到的错误消息:
Uncaught ReferenceError: $rootScope is not defined
Uncaught Error: [$injector:modulerr] Failed to instantiate module wenzey due to:
Error: [$injector:modulerr] Failed to instantiate module wenzey.services due to:
Error: [ng:areq] Argument 'modulesToLoad' is not an array
最佳答案
您对模块使用了错误的定义。您正在将模块与服务混合在一起。您需要了解以下内容才能正确使用它们:
您可以在模块定义中将一个模块指定为其他模块的依赖项。例如
angular .module('wenzy.services', []) .factory('AppService', function () { // Add methods });
现在,您可以在定义模块 wenzy.controllers 时使用模块
wenzy.services
作为依赖项,如下所示:angular .module('wenzy.controllers', ['wenzy.services']);
在定义服务/ Controller 时,您可以将服务注入(inject)到另一个服务/ Controller 中。例如。我们在下面的AppController中注入(inject)了服务
AppService
angular .module('wenzy.controllers', ['wenzy.services']) .controller('AppController', function (AppService) { // Add controller defintion })
$rootScope
是 Angular 提供的服务,可以注入(inject)到任何服务/ Controller 中。您不需要将其添加为模块定义的一部分。
关于javascript - 将 $rootscope 注入(inject)服务模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37103970/