简体   繁体   English

Angular.js-基于环境的动态配置

[英]Angularjs - Dynamic configuration based on Environment

I have a sample angular APP - app.js 我有一个示例角度APP-app.js

angular
    .module('myUiApp', [
        'restangular',
        'ngRoute',
        'ngCookies',
        'ui.bootstrap',
        'ui.sortable',
        'smart-table',
        'config'
    ])
    .config(function($routeProvider, $httpProvider, $sceProvider, $logProvider, RestangularProvider, config) {
        RestangularProvider.setBaseUrl(config.apiBaseUrl);
        RestangularProvider.setDefaultHeaders({'Content-Type': 'application/json'});
//routing here
.....
});

my Config.js looks like - 我的Config.js看起来像-

angular.module('config', []).service('config', function($location, ENV) {
    return ENV.dev;
});

my constants.js looks like - 我的constants.js看起来像-

'use strict';

 angular.module('config', []).constant('ENV', (function() {
    return {
    dev: {
    appBaseUrl:'http://localhost:9000/',
    apiBaseUrl:'http://localhost:8082/api/'
    }
}
})());

I am getting the error saying, Failed to instantiate module myUiApp due to: [$injector:unpr] Unknown provider: config. 我收到错误消息,由于[$ injector:unpr]未知提供程序:config,无法实例化模块myUiApp。

My assumption is injecting config module will invoke the service, which in turn return the json object. 我的假设是注入配置模块将调用该服务,该服务又返回json对象。 any thoughts or suggesstions to do this dynamic config better? 有什么想法或建议可以更好地完成此动态配置?

You can only inject providers into an angular .config() block. 您只能将providers注入有角的.config()块中。 You're attempting to inject a service , and that is likely the cause of your error. 您正在尝试注入service ,这很可能是导致错误的原因。

Also, you have angular.module('config', []) in two different places. 另外,您在两个不同的地方都有angular.module('config', []) This should only be used once to instantiate the module. 只能实例化该模块一次。 Use angular.module('config') (without the second argument) subsequently to reference that module. angular.module('config')使用angular.module('config') (不带第二个参数)来引用该模块。

I would avoid calling the module config , in favor of something that isn't a method used by angular module.config() -- maybe myConfigModule 我会避免调用模块config ,而支持那些不是angular module.config()使用的方法的东西-也许是myConfigModule

Secondly, make sure your script includes the constants.js file and the Config.js file before it includes the app.js file 其次,请确保您的脚本包括constants.js文件和Config.js文件时,它包含app.js文件之前

Lastly double check that this situtation is not affecting you: 最后,仔细检查这种情况不会影响您:

defining the module twice with angular.module('config', []) ( emphasis on the [ ] ..) When you define the module with the square brackets, you are saying "New Module". angular.module('config', [])两次定义模块(强调[] ..)。用方括号定义模块时,您说的是“新模块”。 In the second file that you include, change it to angular.module('config') -- or, combine the files into this: 在您包含的第二个文件中,将其更改为angular.module('config') ,或者将文件合并为以下文件:

angular.module('myConfigModule', [])
.constant('ENV', (function() {
return {
    dev: {
        appBaseUrl:'http://localhost:9000/',
        apiBaseUrl:'http://localhost:8082/api/'
        }
    }
}).service('config', function($location, ENV) {
    return ENV.dev;
});

UPDATE: And typically I see this syntax for controllers, services, anything that is injecting anything else 更新:通常我会看到这种语法用于控制器,服务,注入其他任何东西的任何东西

.service('config', ['$location', 'ENV', function($location, ENV) {
     return ENV.dev;
}]); // see beginning and end of square bracket
//  also add new injected modules to both the array (surrounded by quotes) and the function

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

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