1

Hy I have a div that simulates an input file button. 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. 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? 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

5
  • Are you sure that triggering the click doesn't somehow call this same code in return? Also, can you provide a jsFiddle or something, because I'm not sure I see how this behavior is happening Commented Sep 26, 2014 at 15:03
  • Not sure that I understood your question right, but yes, when I trigger the click it open's a file dialog, if he presses save then the upload is handled, otherwise nothing happens...I'll start working on the fiddle right now Commented Sep 26, 2014 at 15:06
  • My point was that I doubt the $timeout is the source of the problem (unless you're using a version of Angular that has a bug with it). I was suggesting that it's possible the code/function that contains this trigger is being called in recursion, causing the problem. I can't reproduce with a simple jsFiddle (jsfiddle.net/twp3e6h2), so you're going to need to, otherwise it doesn't make sense Commented Sep 26, 2014 at 15:11
  • Thank's for the help, couldn't manage to reproduce the error in a fiddle... quite wird! thank's anyway! Commented Sep 26, 2014 at 15:42
  • 1
    @Ian - I am pretty sure Ian is right. This happened to me recently, I used ng-click on a button to call a controller method which uses $timeout to trigger a click on a file input element, however the file input element is inside the button (hidden w/ display: none) that initiated ng-click, hence triggering an infinite loop. Commented Feb 16, 2019 at 17:05

1 Answer 1

2

why instead of using a $timeout you try to us an $interval and set it to run one time?

var interval =  $interval(function() {
    angular.element('#fileInput').trigger('click');
}, 100,1);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.