简体   繁体   English

将$ rootscope注入服务模块

[英]Inject $rootscope into services module

How would I inject $rootscope into wenzey.services so it is accessible across my application? 我如何将$rootscope注入wenzey.services中,以便可以在我的应用程序中访问它?

(function () {

    'use strict';

    /**
     *
     */
    angular
        .module('wenzey.services', '$rootScope', [])

        $rootScope.type = "consumer";

})();

This is the error message I am currently getting: 这是我当前收到的错误消息:

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

You are using wrong definition for a module. 您为模块使用了错误的定义。 You are mixing a module with a service. 您正在将模块与服务混合在一起。 You need to understand the below to use them properly: 您需要了解以下内容才能正确使用它们:

  1. You specify a module as a dependency of other module in module definition. 您可以在模块定义中将一个模块指定为其他模块的依赖项。 Eg 例如

     angular .module('wenzy.services', []) .factory('AppService', function () { // Add methods }); 

    Now you can use the module wenzy.services as a dependency while defining module wenzy.controllers as below: 现在,您可以在定义模块wenzy.controllers时使用wenzy.services模块作为依赖 ,如下所示:

     angular .module('wenzy.controllers', ['wenzy.services']); 
  2. You inject a service in another service/ controller while defining the service/ controller . 您在定义服务/控制器的同时将服务注入另一个服务/控制器。 Eg We have injected the service AppService in the AppController below 例如,我们在下面的AppController中注入了服务AppService

     angular .module('wenzy.controllers', ['wenzy.services']) .controller('AppController', function (AppService) { // Add controller defintion }) 

The $rootScope is a service provided by angular and can be injected into any service/ controller. $rootScope是由angular提供的服务,可以注入到任何服务/控制器中。 You don't need to add that as part of module definition. 您不需要将其添加为模块定义的一部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM