在app.run AngularJS承诺() [英] AngularJS promise in app.run()

查看:218
本文介绍了在app.run AngularJS承诺()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作angularJs和打字稿项目。我必须做出同步HTTP调用,并从服务器上获取一些数据之前,我启动我的客户端应用程序和加载UI。我搜索互联网上,看到大家关于承诺说话,huumm还好何乐而不为。所以,我在app.run使用的承诺(使$ HTTP调用和使用$ q返回承诺)()。也许我什么都不缺,因为这是行不通的。角发射的app.config(),然后app.run()...但角度不等待的app.config推出app.run()之前完成()。所以,我的第一个承诺是在app.run(发射),并将其解析角度尝试实例化控制器之前......我不希望创建新的服务呼叫httpSynchronous,但我没有任何其他的想法。

I'm working on angularJs and typescript project. I have to make synchronous http call and get some data from server before I launch my client app and load UI. I search on internet and see everybody speak about promise, huumm okay why not. So I use promise (make $http call and use $q to return promise) in my app.run(). Maybe I'm missing nothing because this is not working at all. Angular launch app.config(), then app.run(), ... But Angular does not wait app.config() finish before launch app.run(). So my first promise is launch in app.run() and before it resolve Angular try to instantiate controller... I don't want to create new service call httpSynchronous but I haven't any other ideas.

推荐答案

角不支持异步操作的.config .RUN 功能。如果你想耽误您的应用程序中,有2种方式:

Angular does not support asynchronous actions in .config and .run functions. If you want to delay your application, there are 2 ways:


  • 第一种方法是延迟使用angular.bootstrap整个应用程序()来手动启动它。但是,任何你做的是外围的角度,这样你就无法获得任何东西,但香草的JavaScript。

  • 第二个就是用你的路线的决心财产。如果您使用路由器如角航段或的 UI路由器,您可以定义一个决心,这将可以解决一个顶级路由/状态时,你的应用程序加载(如果你强制路线全重装)。

  • The first one is to delay your whole application by using angular.bootstrap() to manually start it. But anything you do is outside angular, so you don't have access to anything but vanilla JavaScript.
  • The second one is to use the resolve property of your routes. If you use a router like angular route segment or ui router, you can define a top route / state with a resolve which would be resolved when your application loads (and if you force a full reload of your route).

这篇关于在app.run AngularJS承诺()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆