问题描述
我有必须在(或之前)被加载AngularJS的启动对象(这是权限的用户的配置文件,隐藏/显示用户界面的部分地区,检查也做服务器端)。
I have an object that must be loaded within (or before) AngularJS's start up (it is a user's profile of permissions that hides/shows parts of the user interface, checking is also done server side).
我的第一个想法是使用:
My first idea was to use:
module.run(function() {... })
但异步和承诺的性质意味着轮廓可以一切后,被载入。
but the nature of async and promises means the profile could be loaded after everything else.
我的下一个想法是内联的配置文件作为脚本元素:
My next idea was to inline the profile as a script element:
<script type="text/javascript" src="/api/user-profile"></script>
当它返回一个JSON全局变量,如:
Where it returns a JSON global variable like:
var userProfile = { ... };
,然后把用户配置文件到根范围module.run期间()。
and then put the user profile into the root scope during module.run().
这工作得很好,但似乎很脏。
This works well but seems dirty.
基本上,我想之前或角的启动过程中加载数据,就像您在路线的决心,因为它继续前等待。
Basically, I want to load data before or during Angular's startup, like you would with resolve in routes, in that it waits before continuing.
有一个更清洁的方式?也许,某种角度的服务?
Is there a cleaner approach? Perhaps some kind of Angular service?
推荐答案
您可以手动启动与 angular.bootstrap
Angular.js。所以,你可以先加载数据,那么当你觉得准备好了。呼叫 angular.bootstrap()
启动角模块。
You could manually start Angular.js with angular.bootstrap
. So that you can load your data first, then when you feel ready. Call angular.bootstrap()
to start angular module.
您必须prepare什么:
What you have to prepare:
- 删除
NG-应用=XXX
从您的文档。 - 呼叫
angular.bootstrap()
一切准备就绪之后。
- Remove
ng-app="XXX"
from your document. - Call
angular.bootstrap()
after everything is ready.
的 的
这篇关于在应用程序启动时加载AngularJS数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!