angularjs – 在初始化角度app之前等待$http结果

前端之家收集整理的这篇文章主要介绍了angularjs – 在初始化角度app之前等待$http结果前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个角度应用程序,需要做一个快速的http请求,以便在启动应用程序的其余部分之前获取一些配置信息,至少在控制器之前.看着使用$UrlRouterProvider,但我没弄明白如何使应用等待http完成.

我需要完成的事情:

  1. $http({method: 'GET',url: '/config'}).then(function(res) {
  2. configProvider.setConfig(res.data.config);
  3. }
您可以创建一个单独的js文件,您可以在其中生成http请求,然后通过js代码而不是ng-app在HTML代码中初始化/引导您的应用程序.

请参考以下代码

  1. (function() {
  2. var application,bootstrapApplication,fetchData;
  3. application = angular.module('app');
  4. fetchData = function() {
  5. var $http,initInjector;
  6. initInjector = angular.injector(['ng']);
  7. $http = initInjector.get('$http');
  8. $http.get('<url>');
  9. };
  10. bootstrapApplication = function() {
  11. angular.element(document).ready(function() {
  12. angular.bootstrap(document,['app']);
  13. });
  14. };
  15. fetchData().then(bootstrapApplication);
  16. })();

我希望它有所帮助.

猜你在找的Angularjs相关文章