我有一个示例角度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
.....
});
我的Config.js看起来像 -
angular.module('config', []).service('config', function($location, ENV) {
return ENV.dev;
});
我的constants.js看起来像 -
'use strict';
angular.module('config', []).constant('ENV', (function() {
return {
dev: {
appBaseUrl:'http://localhost:9000/',
apiBaseUrl:'http://localhost:8082/api/'
}
}
})());
我收到的错误是,无法实例化模块myUiApp,原因是: [$ injector:unpr]未知提供者:config。
我的假设是注入配置模块将调用服务,而服务又返回json对象。任何想法或建议更好地做这个动态配置?
答案 0 :(得分:0)
您只能将providers
注入角度.config()
块。您试图注入service
,这可能是导致错误的原因。
此外,您在两个不同的地方angular.module('config', [])
。这应该只用一次来实例化模块。随后使用angular.module('config')
(不带第二个参数)来引用该模块。
答案 1 :(得分:0)
我会避免调用模块config
,而不是使用角度module.config()
使用的方法 - 可能myConfigModule
其次,在包含app.js文件之前,请确保您的脚本包含constants.js
文件和Config.js
文件
最后仔细检查一下这种情况对你没有影响:
使用angular.module('config', [])
两次定义模块(强调 [] ..)当您使用方括号定义模块时,您说的是“新模块”。在您包含的第二个文件中,将其更改为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;
});
更新:通常我会看到控制器,服务以及注入其他任何内容的任何内容的语法
.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