Angular js Input type file - clear previously selected file
32,927
This was the easiest way, without using any directive or complex code.
Browse <input type="file" ng-click="clear()"
And in your controller
$scope.clear = function () {
angular.element("input[type='file']").val(null);
};
Author by
KeenUser
Updated on July 19, 2022Comments
-
KeenUser almost 2 years
I am using
input type="file"
for my upload control using angular js. Everytime I click on browse, I do not want to see the previously selected file. By default,this seems to be retained. Can it be achieved by writing a directive? Can it be triggered everytime I click on browse?I am using a bootstrap implementation to replace default browse button with some better.
<span class="useBootstrap btn btn-default btn-file"> Browse <input type="file" /> </span>
-
user804401 almost 9 yearssee if this helps stackoverflow.com/questions/15079779/…
-
-
Ch Faizan Mustansar over 8 yearsYes, indeed this is an easiest way.... But it is not the right way. I have gone through the trouble of rewriting the whole code once again in the form of directives and all, thats why I cannot recommend this approach.
-
Eddy over 7 yearsThis complained that it cannot use jqLite for selectors. But this worked fine: var myEl = angular.element( document.querySelector( '#some-id' ) );
-
Mawg says reinstate Monica over 7 yearsIf you want a separate
Clear
orCancel
button, add anid=
to the element. E.g.id="fileSelector_1" and then
angular.element(document.querySelector('#fileSelector_1')).val(null);` This works for me. I use it to clear on both theSave
andCancel
buttons,