Home > database >  Angular file input change event not firing at all
Angular file input change event not firing at all

Time:06-08

Has anyone used a file input with Angular. I'm not able to get the change event to fire when declared as (change), but when using onchange it works but not with the angular method. Has anyone successfully used (change) event? Not sure what I'm missing here.

This works...

<input hidden mat-input onclick="this.value = null" onchange=”alert(‘works’)” #fileInput type="file">

This doesn't work...

<input hidden mat-input onclick="this.value = null" (change)=”alert(‘works’)”  #fileInput type="file">

CodePudding user response:

<input mat-input (change)="onChange()" #fileInput type="file" />

CodePudding user response:

//app.component.html

<div>
   <label for="file">file</label>
   <input hidden id="file" #file name="file" type="file" (change)="onFileSelected($event)">
</div>


//app.component.ts

onFileSelected(event){
  const file = event.target.files[0];
  //do what you want with the event
}
  • Related