Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to trigger 'click' event of on a file input in Angular?

Tags:

angularjs

here is the view in jade:

button#save-csv-btn(ng-click="click()") Export CSV
input#save-csv(style="display:none", type="file", onchange="angular.element(this).scope().saveCSVFileChanged(this)")

js:

$scope.click = ->
    # $('#save-csv').trigger('click')

Error I get:

Error: $apply already in progress
like image 728
AZ. Avatar asked Aug 22 '13 00:08

AZ.


People also ask

How can I trigger the Click event of another element in NG click using Angularjs?

The syntax is the following: function clickOnUpload() { $timeout(function() { angular. element('#myselector'). triggerHandler('click'); }); }; // Using Angular Extend angular.


1 Answers

I changed $scope.click function to trigger the input click in a setTimeout. This lets the first $apply finish, and then will trigger another one.

$scope.click = function() {
    setTimeout(function() {
        inputEl.click();
    }, 0);
}

Note that I use setTimeout, not $timeout. $timeout would also be inside an $apply block.

like image 69
mcfedr Avatar answered Oct 30 '22 23:10

mcfedr