我有一个必须继承的继承的应用程序。到目前为止,它仅在一台服务器上运行,而我们想在另一台服务器上运行它。我们在python服务器代码和C ++客户端代码中到处都找到了对服务器名称的硬编码引用。更改这些名称以从配置文件读取服务器名称非常容易。但是现在我发现是一个js模块的代码:
angular.module('app.config', []).constant('config', {"api":"http://foo.bar.com:8001/"});
如何更改此设置,以便可以从配置文件中动态读取服务器的值,或者通过其他一些不需要对其进行硬编码的方式进行读取?
这是我尝试过的更新:
最初,我的代码具有以下内容:
angular.module('app.config', []).constant(
'config', {"api":"http://foo.bar.com:8001/"}
);
我将其更改为:
angular.module('app.config').controller('myCtrl',
function($scope, $http) {
$http.get('./config.json')
.then(function(response) {
$scope.api = response.data;
});
});
我得到了这个:
error Module 'app.config' is not available!
然后我将其更改为:
angular.module('app.config', [] ).controller('myCtrl',
function($scope, $http) {
$http.get('./config.json')
.then(function(response) {
$scope.api = response.data;
});
});
然后我得到:
Error: [$injector:unpr] Unknown provider: configProvider <- config <- workitem
我觉得我很亲密,我只需要更多帮助。
另一个更新:
我尝试了这个:
angular.module('app').controller('home', ['$scope', homeCtrl]);
angular.module('app').controller('workitem', ['$scope', '$routeParams', '$sce', '$timeout', '$http', 'config', workitemCtrl]);
},{"./home/home.js":3,"./workitem/workitem.js":4,"angular":10,"angular-route":6,"angular-sanitize":8,"bootstrap-treeview/src/js/bootstrap-treeview.js":11}],2:[function(require,module,exports){
module.exports = function($scope,$http) {
$http.get('config.json').success(function(reponse) {
console.log("reponse --> "+reponse.api);
$scope.api = reponse.api;
});
}
但是,当然,没有定义app.config。我该如何仍然定义app.config?
我只是试过这个:
var my_config = {};
$.getJSON("config.json", function(data) {
$.each(data, function(key, val) {
my_config[key] = val;
});
});
但是我在控制器中使用my_config时未定义它。如何使该变量在控制器中可用?
我终于可以通过预先完成此工作:
angular.module('app').value('config', {
api: ''
});
angular.module('app').run(function($rootScope, $http, config) {
$http.get('config/config.json').then(function(response) {
config.api = response.data.api;
$rootScope.$broadcast('config-loaded');
});
});
将主要代码包装在:
var init = function(){
}
并在末尾添加:
if (config.api) {
init()
} else {
var configLoaded = scope.$on('config-loaded', function() {
init();
configLoaded();
});
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句