使用ngModel访问输入文件[] [英] Access input files[] using ngModel

查看:63
本文介绍了使用ngModel访问输入文件[]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的表单中有一个图片上传输入:

I have an image upload input in my form:

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

是否可以使用item.image ngModel访问#image.files[0](而不是创建引用)?

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

如果不是,那么当输入类型为file时ngModel仍然存储什么?

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

推荐答案

是否可以使用item.image ngModel访问#image.files [0](而不是创建引用)?

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

是的,您可以订阅输入的change事件并将文件传递给方法:

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])" />

当输入类型为文件时,ngModel仍然存储什么?

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

什么都没有. DefaultValueAccessor 不适用于input[type="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.

这篇关于使用ngModel访问输入文件[]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆