I'm trying to trigger the click event of the <input type="file">
element from the button
.
<input id="upload"
type="file"
ng-file-select="onFileSelect($files)"
style="display: none;">
<button type="button"
ng-click="angular.element('#upload').trigger('click');">Upload</button>
It's common practice to hide the uglified beast known as <input type=file>
and trigger it's click event by some other means.
So it was a simple fix. Just had to move the
ng-click
to a scope click handler:If you are getting $scope binding errors make sure you wrap the click event code on a setTimeout Function.
VIEW
CONTROLLER
If your input and button are siblings (and they are in your case OP):
Use a directive to bind the click of your button to the file input like so:
I just came across this problem and have written a solution for those of you who are using Angular. You can write a custom directive composed of a container, a button, and an input element with type file. With CSS you then place the input over the custom button but with opacity 0. You set the containers height and width to exactly the offset width and height of the button and the input's height and width to 100% of the container.
the directive
a jade template if you are using jade
the same template in html if you are using html
the css
I think you are over complicated things a bit. Do you really need to trigger a click on the input from your button ?
I suggest you just apply a proper style to your input and the ngFileSelect directive will do the rest and call your onFileSelect function whenever a file is submitted :
I took the answer posted by Osiloke (Which was the easiest and most complete imho) and I added a change event listener. Works great! Thanks Osiloke. See below if you are interested:
HTML:
Directive:
Controller: