Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm getting the error [$rootScope:inprog] $apply already in progress whenever I trigger a click event on a hidden file upload input.

HTML:

<!-- button that calls function in controller -->
<button type="button" ng-click="uploadClicked()">Upload</button>
<!-- Upload input I'm triggering the click event on -->
<input type="file" accept="image/*" id="profile-photo" name="profile-photo" ng-hide="true"/>

AngularJS function in controller:

$scope.uploadClicked = function () {
    //Causes Error: [$rootScope:inprog] $apply already in progress and opens file dialog
    document.getElementById('profile-photo').click();

    //Causes Error: [$rootScope:inprog] $apply already in progress and does not open file dialog
    $('#profile-photo').trigger('click');
};

I'm not calling $apply or $digest anywhere in my controller. Why would this error be occurring?

share|improve this question
    
Because angularjs does some actions in the background and your tiggered click conflicts at that place. –  t.niese Jan 3 at 11:11
    
That's rather unfortunate. Any advice on how to circumvent this? –  Jack Zach Tibbles Jan 3 at 11:12
    
Try wrapping it in a $timeout –  Rob J Jan 3 at 11:18
    
@justine have you had any luck with this ?I am facing the same issue.I am manually clicking on a hidden link for downloading and this error is thrown then.Please update –  Divya MV Mar 3 at 9:51

1 Answer 1

I just got a similar issue and i was able to fix it by wrapping the place where we click with a

 setTimeOut(function(){uploadClicked();},0);

I guess by using it angular will be aware of that method and will not initialize another digest cycle on finding an click event.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.