最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - NgModel on Input type file - Stack Overflow

programmeradmin3浏览0评论

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:

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

Share Improve this question edited Jan 18, 2018 at 12:05 Kamil Naja 6,6926 gold badges37 silver badges51 bronze badges asked Jan 18, 2018 at 11:59 LeonzenLeonzen 1,2755 gold badges14 silver badges31 bronze badges 1
  • 2 Possible duplicate of Angular 2 File upload from input type=file – Akash Agrawal Commented Jan 18, 2018 at 12:03
Add a comment  | 

1 Answer 1

Reset to default 19

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);
  }
发布评论

评论列表(0)

  1. 暂无评论