AngularJS - 运行code当所有其他控制器和这样完成 [英] AngularJS - run code when all other controllers and such are done
问题描述
我是新来的角
,并在尝试做一些网页拦截,而内容的负载。此内容包括 AJAX
请求和其他一些东西,都裹成的控制器。
I am new to Angular
, and am attempting to do some page blocking while content loads. This content includes ajax
requests and some other things, all wrapped into controllers.
previously,我用一个简单的排队系统中使用 $。递延
jQuery中处理这一点,但因为一路节目潮的角是根本......不是那样的,我在一个有点损失。我有一个办法很难思维告诉我的程序/模块/应用程序,是的,我做的加载,你现在已经完成了。
Previously, I was handling this with a simple queue system using $.Deferred
in jQuery, but since the way the program "flows" in Angular is fundamentally ... not like that, I'm at a bit of a loss. I'm having a hard time thinking of a way to "tell" my program/module/app that "Yes. I am done loading. You are finished now".
有没有其他人经历过这个,发现一个解决方案呢?
Has anyone else experienced this and discovered a solution to it?
我首先想到的是只需要调用的事件在每个控制器的结束,但已经取得了一些不同的结果。必须有一个更专业,可靠的解决方案。
My first thought was to just call events at the end of each controller, but that has yielded some mixed results. There has to be a more professional, reliable solution.
推荐答案
ngRouter模块允许只是做与决心财产。这将延缓视图呈现直到所有数据都loadedThe完整的文档可以在这里找到:
https://docs.angularjs.org/api/ngRoute
ngRouter module permits to just do that with the resolve property. It will delay view rendering till all data is loadedThe complete doc can be found here: https://docs.angularjs.org/api/ngRoute
一个基本的例子(从DOC):
A basic example (from the doc):
angular.module('yourApp', ['ngRoute']);
angular.module('yourApp')
.config(function($routeProvider) {
$routeProvider
.when('/Book/:bookId', {
templateUrl: 'book.html',
controller: 'BookController',
resolve: {
// I will cause a 1 second delay
delay: function ($q, $timeout) {
var delay = $q.defer();
$timeout(delay.resolve, 1000);
return delay.promise;
}
}
})
.when('/Book/:bookId/ch/:chapterId', {
templateUrl: 'chapter.html',
controller: 'ChapterController'
});
});
承诺延迟得到解决之前,您的观点将不会被渲染。
Your view will not be rendered before the promise delay is resolved.
这篇关于AngularJS - 运行code当所有其他控制器和这样完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!