我在哪里可以初始化模块范围的变量? [英] Where can I initialize module-wide variables?

查看:137
本文介绍了我在哪里可以初始化模块范围的变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做这样的事情:

angular.module('MyModule', ['ui'])
    .config(function($rootScope) {
        $rootScope.Gender = {
            'M': 'Male',
            'F': 'Female',
            'U': 'Unknown',
        };
    })

但我得到这个错误:

But I get this error:

未捕获的错误:未知提供商:从MyModule的$ rootScope

Uncaught Error: Unknown provider: $rootScope from MyModule

如果我无法访问 $ rootScope 我的模块配置,哪来的初始化模块范围的变量合适的地方里面呢?

If I can't access $rootScope inside my module config, where's the proper place to initialize module-wide variables?

推荐答案

您不能注入服务(这里$ rootScope)到配置块。只有常量和提供者可以在配置阶段被注入。

You can't inject services (here $rootScope) into config block. Only constants and providers can be injected during the config phase.

在您的情况下正确的解决办法是使用运行块。只要改变配置来运行和东西都应该按预期工作。

In your case the correct solution would be to use the run block. Just change config to run and things should be working as expected.

这篇关于我在哪里可以初始化模块范围的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆