Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access input files[] using ngModel

Tags:

angular

I have an image upload input in my form:

<input type="file" [(ngModel)]="item.image" name="image" #image>

Is it possible to access #image.files[0] using item.image ngModel (instead of creating a reference)?

If not, what does ngModel stores anyway when the input type is file?

like image 369
TheUnreal Avatar asked Feb 06 '23 19:02

TheUnreal


1 Answers

Is it possible to access #image.files[0] using item.image ngModel (instead of creating a reference)?

Yes, you can subscribe to the change event of the input and pass the file to the method:

<input type="file" name="image" (change)="fileSelected($event.target.files[0])" />

What does ngModel stores anyway when the input type is file?

Nothing. DefaultValueAccessor does not work with input[type="file"] and I don't think there is a specific value accessor for it at the moment.

like image 152
Zabavsky Avatar answered Feb 24 '23 04:02

Zabavsky