我知道这个问题很老,但由于没有公认的答案,也没有一个干净的答案,我也在尝试解决这个问题。
每个应用程序缺乏解析状态让我困扰了一段时间,这是我最终想出的解决方案:http: //jsfiddle.net/gabrielcatalin/cvyq0oys/
简而言之,我创建了一个 Angular Module Enhancer(decorator),它支持另外 2 种方法:resolve()和ready()。AngularEnhancer 片段只需要在 angular.js 加载之后和实现之前加载,如下所示:
<script type="text/javascript" src="angular.js">
<script type="text/javascript" src="angularEnhancer.js">
<script type="text/javascript" src="main.js">
这是实现的外观:
angular.module('myApp', [])
.ready(function() {
alert('The app is ready to be used!');
})
.resolve(['$q', function ($q) {
// The resolve can wait for a promise to be resolved
var differed = $q.defer();
setTimeout(function() {
differed.resolve();
}, 1000);
return differed.promise;
// Or a true value
// return true;
// Or even a truish value
// return 'value';
}])
.ready(function() {
// The order of where the ready() appear doesn't matter
alert('This is just another alert to show that it can have as many as possible!');
});
和html:
<body>
<div ng-if="!appReady" class="splash-screen">This is just the splash screen!</div>
<div ng-if="appReady">Welcome to the app!</div>
<script>
angular.element(document).ready(function() {
angular.bootstrap(document, ['myApp']);
});
</script>
</body>
希望看到大家的一些意见!