NgModel on Input type file

自闭症网瘾萝莉.ら 提交于 2020-01-23 09:09:31

问题


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">

and

files:any

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


回答1:


You have to do it externally through (change) event

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

And access in the ts file like the below code

  files: any[];

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


来源:https://stackoverflow.com/questions/48320823/ngmodel-on-input-type-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!