输入类型文件上的NgModel [英] NgModel on Input type file

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

问题描述

我正在尝试通过ngModel这样的典型Angular方式绑定到输入字段类型文件:

I'm trying to make a binding to an input field type file through ngModel on the typical Angular way like this:

<input type="file" id="fileUpload" [(ngModel)]="file">

files:any

我的问题是选择文件后,变量files的值仍然是undefined 以下是关于stackblitz的示例: https://stackblitz.com/edit/angular-6mbdww

My problem is that after I have chosen a file, the value of my variable files is still undefined Here an example with stackblitz: https://stackblitz.com/edit/angular-6mbdww

推荐答案

您必须通过(更改)事件从外部进行操作

You have to do it externally through (change) event

<input (change)="onFileChange($event)" type="file" id="fileUpload">

然后像下面的代码一样访问ts文件

And access in the ts file like the below code

  files: any[];

  onFileChange(event){
    this.files = event.target.files;
    console.log(event);
  }

这篇关于输入类型文件上的NgModel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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