Javascript 在AngularJS的配置块中加载数据
在app.config块中加载数据的正确(角度)方法是什么 引导模块+提供程序会有帮助吗?我尝试了以下方法(简化),但没有成功Javascript 在AngularJS的配置块中加载数据,javascript,angularjs,Javascript,Angularjs,在app.config块中加载数据的正确(角度)方法是什么 引导模块+提供程序会有帮助吗?我尝试了以下方法(简化),但没有成功 angular.module('boot',[]).provider('test', function(){ this.$get = function() { return { getString: function() { return "i am a string"; } } }; }); angular.module('m
angular.module('boot',[]).provider('test', function(){
this.$get = function() {
return {
getString: function() { return "i am a string"; }
}
};
});
angular.module('main',['boot']).config(['testProvider', function(test) {
//no luck with getString
}]);
现实情况是,我希望在配置$routeProvider之前加载本地化路由并确定当前语言。
TIA。您当前的代码使用getString方法返回一个测试服务。为了在提供程序中获取字符串(),您应该执行以下操作
angular.module('boot',[]).provider('test', function(){
// Method available in testProvider
this.getString = function () {
return "i am a string";
}
this.$get = function() {
return {
// Your test service.
}
};
});
angular.module('main',['boot']).config(['testProvider', function(test) {
var myString = testProvider.getString();
}]);
好的,谢谢,现在我明白了提供者的工作原理:)。不过,我仍然无法解决我的问题,无法向我的提供者注入$http。我应该如何加载数据以使其在我的配置块中可用并为$routeProvider提供数据?您不能在配置期间注入$http,它在加载这些服务之前运行。我看你有两个选择。1) 在构建过程中加载正确的配置(是否使用grunt?),2)使用这样的方法!我需要eazel7在最后一个答案中解释的内容。谢谢。