我有一个角度应用程序,需要做一个快速的http请求,以便在启动应用程序的其余部分之前获取一些配置信息,至少在控制器之前.看着使用$UrlRouterProvider,但我没弄明白如何使应用等待http完成.
我需要完成的事情:
- $http({method: 'GET',url: '/config'}).then(function(res) {
- configProvider.setConfig(res.data.config);
- }
您可以创建一个单独的js文件,您可以在其中生成http请求,然后通过js代码而不是ng-app在HTML代码中初始化/引导您的应用程序.
请参考以下代码:
- (function() {
- var application,bootstrapApplication,fetchData;
- application = angular.module('app');
- fetchData = function() {
- var $http,initInjector;
- initInjector = angular.injector(['ng']);
- $http = initInjector.get('$http');
- $http.get('<url>');
- };
- bootstrapApplication = function() {
- angular.element(document).ready(function() {
- angular.bootstrap(document,['app']);
- });
- };
- fetchData().then(bootstrapApplication);
- })();
我希望它有所帮助.