javascript Angular js - 显示/隐藏每条新路线的加载 gif
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21512893/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Angular js - show/hide loading gif every new route
提问by itsme
I'm trying to toggle (hide/show) a loading gif on every new route, so my logic would be:
我试图在每条新路线上切换(隐藏/显示)加载 gif,所以我的逻辑是:
- routeChangeStart = show loading gif
- routeChangeSuccess = hide loading gif
- routeChangeStart = 显示加载 gif
- routeChangeSuccess = 隐藏加载 gif
This is my code:
这是我的代码:
//ANGULAR
app.run(function($rootScope) {
$rootScope.layout = {};
$rootScope.layout.loading = false;
$rootScope.$on('$routeChangeStart', function() {
//show loading gif
$rootScope.layout.loading = true;
});
$rootScope.$on('$routeChangeSuccess', function() {
//hide loading gif
$rootScope.layout.loading = false;
});
$rootScope.$on('$routeChangeError', function() {
//hide loading gif
alert('wtff');
$rootScope.layout.loading = false;
});
});
//HTML
//HTML
<img src="img/loading.gif" ng-hide="!layout.loading"/>
it is strange cause this works for 3/4 routes changed then it stop working while changing routes :O
这很奇怪,因为这适用于更改了 3/4 条路线,然后在更改路线时停止工作:O
what it could be?
它可能是什么?
HERE IS A LIVE EXAMPLE thanks to @Rob Sedgwick : http://plnkr.co/edit/ZpkgRhEAoUGlnXjbLb8b
感谢@Rob Sedgwick,这是一个活生生的例子:http://plnkr.co/edit/ZpkgRhEAoUGlnXjbLb8b
回答by michael
Short answer: the $rootScope.layout.loading
changed as expected. But if the templates are loaded the browser has no time to make the changes visible.
简短的回答:$rootScope.layout.loading
如预期的那样改变了。但是,如果加载了模板,浏览器就没有时间使更改可见。
Long answer: if you put console.log
outputs in your $on
listeners you will see, that a $routeChangeStart
event fires, the template is loaded and after that $routeChangeSuccess
fires. Under the hood the following happens:
长答案:如果你把console.log
输出放在你的$on
监听器中,你会看到,一个$routeChangeStart
事件触发,模板被加载,然后$routeChangeSuccess
触发。在引擎盖下会发生以下情况:
$routeChangeStart
fires$rootScope.layout.loading
becomestrue
- the template gets loaded asynchronously
- the DOM is updated
- the browser renders the loading gif
$routeChangeStart
火灾$rootScope.layout.loading
变成true
- 模板被异步加载
- DOM 已更新
- 浏览器呈现加载 gif
a little bit later (the xhr request returns)
稍后(xhr 请求返回)
- the template was loaded
$routeChangeSuccess
fires$rootScope.layout.loading
becomesfalse
- the DOM is updated
- the browser did no longer show the gif
- 模板已加载
$routeChangeSuccess
火灾$rootScope.layout.loading
变成false
- DOM 已更新
- 浏览器不再显示 gif
what happens if the tmeplates are cached:
- $routeChangeStart
fires
- $rootScope.layout.loading
becomes true
- $routeChangeSuccess
fires
- $rootScope.layout.loading
becomes false
- the DOM is updated (but this is the same state as before)
如果 tmeplates 被缓存会发生什么: -$routeChangeStart
触发 -$rootScope.layout.loading
变为true
-$routeChangeSuccess
触发 -$rootScope.layout.loading
变为false
- DOM 被更新(但这与之前的状态相同)
As you can see the browser can not render the new state because a script is running that is not interrupted to give the browser time to render the DOM. If the template is not yet loaded, the ajax requests stops the script execution and give's the browser time to render the DOM.
正如您所看到的,浏览器无法呈现新状态,因为脚本正在运行而不会中断,从而为浏览器提供了呈现 DOM 的时间。如果模板尚未加载,ajax 请求会停止脚本执行并给浏览器时间来呈现 DOM。
How to fix this?
如何解决这个问题?
You can use a timeout
that give's the browser time to update the DOM:
你可以使用一个timeout
给浏览器时间来更新 DOM 的:
$rootScope.$on('$routeChangeSuccess', function () {
console.log('$routeChangeSuccess');
//hide loading gif
$timeout(function(){
$rootScope.layout.loading = false;
}, 200);
});
Here is your plunkr with this solution: http://plnkr.co/edit/MkREo0Xpz5SUWg0lFCdu?p=preview
这是您使用此解决方案的 plunkr:http://plnkr.co/edit/MkREo0Xpz5SUWg0lFCdu?p=preview
回答by Rob Sedgwick
( A little too much to put in a comment )
(评论有点太多了)
I set the code up and saw that the loading variable was not updated each time and was due to the template caching, I guess the 'RouteChange' is not triggered.
我设置了代码,看到加载变量每次都没有更新,是由于模板缓存,我猜没有触发“RouteChange”。
Disabling the template caching will let your code run each time ..
禁用模板缓存将使您的代码每次运行..
app.run(function($rootScope, $location, $anchorScroll, $routeParams,$templateCache) {
$rootScope.$on('$viewContentLoaded', function() {
$templateCache.removeAll();
});
....