在初始化角度应用之前等待$ http结果

Sla*_*hor 8 http angularjs angular-ui-router

我有一个角度应用程序需要做一个快速的http请求,以便在启动应用程序的其余部分之前获取一些配置信息,至少在控制器之前.看着使用$ UrlRouterProvider,但我没弄明白如何让应用程序等待http完成.

我需要完成的事情:

 $http({method: 'GET', url: '/config'}).then(function(res) {
      configProvider.setConfig(res.data.config);
 }
Run Code Online (Sandbox Code Playgroud)

Har*_*eet 1

您可以创建一个单独的 js 文件,在其中可以发出 http 请求,然后通过 js 代码而不是 html 代码中的 ng-app 初始化/引导您的应用程序。

参考下面的代码:

(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);
})();
Run Code Online (Sandbox Code Playgroud)

我希望它有帮助。