如何注入模块并使其可访问 Angular 应用程序 [英] How to inject module and make it accesible to entrie angular app

查看:25
本文介绍了如何注入模块并使其可访问 Angular 应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模块 (app.config),我想注入我的整个应用程序.

I have a module (app.config) that I would like to inject my entire app.

该模块需要可在注入应用程序的所有其他模块中访问

例如,我的应用如下所示:

For example, my app looks like this:

angular.module('myApp', [
    'app.config',
    'module#1',
    'module#2',
    'module#3',
    'module#4'    
])
.config...

/////////////////////////////////

/////////////////////////////////

这里是 app.config

angular.module('app.config', []).
    constant('NAME1', 'Name1').
    constant('NAME2', 'Name2'); 
////////////////////

我希望以一种可以在所有模块(module#1','module#2',....)中访问的方式注入'app.config'.

I want 'app.config' injected in such a way that it can be accessed inside all modules (module#1','module#2',....) as well.

这是我的问题:

angular.module('module#1', []).
    service('serviceOne', serviceOne);

function ServiceOne($http) {

    var service = {
        getMyProfile: function(){return $http.get('api/' + NAME1);}
    };

    return service;
}

问题 -> NAME1 未定义. 但我以为我将它注入到整个应用程序???

Problem -> NAME1 is undefined. But I thought I injected it to the entire app???

我不想将 app.config 单独注入每个模块.还有其他解决方案吗?

推荐答案

您还需要将常量注入到控制器中.

you need to inject the constants in to the controller as well.

function ServiceOne($http, NAME1) {

   var service = {...
   ...

 }

这里有一个很好的说明

这篇关于如何注入模块并使其可访问 Angular 应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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