使$ timeout函数起作用的问题 [英] Issues with getting $timeout function to work
问题描述
我用自己的代码创建了一个punkr,它可以正常工作.通过单击切换折叠"按钮折叠下拉菜单后,我需要下拉菜单在3秒后自行关闭.我在example.js的HeaderCtrl中玩了以下游戏,但是没有运气:
I have created a plunkr with my code that does work. After the drop down is collapsed by clicking the Toggle collapse button, I need the dropdown to close on it's own after 3 seconds. I have played with the following in the HeaderCtrl in example.js with no luck:
function callAtTimeout(){
$scope.isFooCollapsed = true;
}
$timeout(function(){
!$scope.callAtTimeout();
}, 3000);
http://plnkr.co/edit/wMxA4Tkiqr9BsSfxia02?p=preview
任何帮助/输入将不胜感激.预先感谢!
Any help/input would be appreciated. Thanks in advance!
推荐答案
您可以仅使用有角的 $ timeout
来实现所需的目的,
You can just use angular $timeout
to achieve the wanted like this:
var timer;
$scope.isFooCollapsed = true;
$rootScope.$on("bagNotification", function() {
$timeout.cancel(timer);
$scope.isFooCollapsed = !$scope.isFooCollapsed;
timer = $timeout(function() {
$scope.isFooCollapsed = true;
}, 3000);
});
我们每次使用 $ timeout.cancel
取消超时,以防止多次单击按钮时多次隐藏/显示.
We cancel the timeout each time with $timeout.cancel
to prevent multiple hide/shows when button is clicked multiple times.
Plunkr: http://plnkr.co/edit/YbsCuicDiHVDGULwpap3?p=preview
这篇关于使$ timeout函数起作用的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!