Issue
I want to upload an image and create a preview of it in angular 4.
The template is given below:
<div>
<input type="file" (onchange)="handleUpload($event)">
<img [src]="Logo" >
</div
I want to call a function handleUpload()
whenever a file is chosen. This function sets the value of the variable Logo
to the path of the file chosen. I am not able to get the path because the function handleUpload($event)
is not called.
The component class is given below:
export class App {
Logo:string;
handleUpload(e):void{
this.Logo = e.target.value;
}
constructor() {
}
}
I don't understand what is wrong with my code. Demo
Solution
In angular you write javascript events without the "on" suffix.
Change:
<input type="file" (onchange)="handleUpload($event)">
To:
<input type="file" (change)="handleUpload($event)">
Answered By - unitario
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.