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>
import { ViewChild } from '@angular/core'; ViewChild allows you to set a reference variable to your input, using that you can clear the value of input. After clearing the value of input using the reference variable, the selected file will be reset.
To reset a file input in React, set the input's value to null in your handleChange function, e.g. event. target. value = null . Setting the element's value property to null resets the file input.
If you are using [(ngModel)] directive to control your form input fields, then you can clear it by setting an empty string ( ' ' ) to the ngModel control property.
The only way to set the value of a file input is by the user to select a file. This is done for security reasons. Otherwise you would be able to create a JavaScript that automatically uploads a specific file from the client's computer.
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);
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With