简体   繁体   English

AngularJS $超时生成无限循环

[英]Angularjs $timeout generating infinite loop

Hy I have a div that simulates an input file button. Hy,我有一个模拟输入文件按钮的div。 To achieve this I did 为了做到这一点,我做了

angular.element('#fileInput').trigger('click');

but this generated an Apply already in progress error, googling around I found that this could easily be avoided putting the code inside a timeout. 但这会产生一个“正在应用”错误,我在谷歌上搜索发现可以很容易地避免将代码放入超时。

timeOut = $timeout(function(){
        angular.element('#fileInput').trigger('click');
    });

And as an effect that really solved the problem, but $timeout generates an infinite loop opening infinite file dialogs if my pop-up block is disabled. 作为一种确实解决了问题的效果,但是$ timeout会生成一个无限循环,如果禁用了我的弹出窗口,则会打开无限文件对话框。 In the AngularJS docs you can clearly read that $timeout is a wrapper of setTimeout wich should only generate one call to the callback function, so why is it generating an infinite loop? 在AngularJS文档中,您可以清楚地了解到$ timeout是setTimeout的包装,它只能生成对回调函数的一个调用,因此为什么生成无限循环? However, trying to solve the situtation I decided to kill the timer after the first call, but I couldn't manage, 但是,为了解决这种情况,我决定在第一次通话后取消计时器,但我无法解决,

timeOut = $timeout(function(){
    angular.element('#fileInput').trigger('click');
});
timeOut.then( function( ){$timeout.cancel(timeOut);
    }
);

I'm getting quite stuck in this situation... I'm I just missing something obvious things? 在这种情况下,我陷入了困境...我只是想丢掉一些明显的东西? Someone has any idea's? 有人有什么主意吗? thank's 谢谢

why instead of using a $timeout you try to us an $interval and set it to run one time? 为什么不使用$ timeout而是尝试给我们一个$ interval并将其设置为运行一次?

var interval =  $interval(function() {
    angular.element('#fileInput').trigger('click');
}, 100,1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM