angularjs – 在初始化角度app之前等待$http结果
发布时间:2020-12-17 07:28:18 所属栏目:安全 来源:网络整理
导读:我有一个角度应用程序,需要做一个快速的http请求,以便在启动应用程序的其余部分之前获取一些配置信息,至少在控制器之前.看着使用$UrlRouterProvider,但我没弄明白如何使应用等待http完成. 我需要完成的事情: $http({method: 'GET',url: '/config'}).then(fu
我有一个角度应用程序,需要做一个快速的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); })(); 我希望它有所帮助. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |