Hoping someone can teach me something new, I'll try to keep it short since I'm betting it's just some inane detail I'm hopefully missing...
I start with an image like so, where currentImage is an existing image path to act as a default at first;
<img src="{{currentImage}}">
<input type="file" (click)="onUpload($event)">
Which then fires off a basic method to grab the event.files etc like so;
onUpload(event) {
const file: File = event.files[0],
reader: FileReader = new FileReader(),
that = this; // *** WORTH NOTING...
reader.addEventListener('loadend', function () {
console.log('the resulting base64 string = ', reader.result);
that.currentImage = reader.result;
}, false);
if (file) {
reader.readAsDataURL(file);
}
console.log('does the blob get updated to currentimage?',
this.currentImage); // THIS RETURNS THE OLD IMAGE PATH***
}
This sort of works, I see the base64 string I want in the console, I see the currentImage var string update to the base64 string in the html binding {{currentImage}}, and I see the img element flash the newly updated image...but then it immediately reverts right back to the default image originally assigned to currentImage even though currentImage still has the new base64 string.... Also the last console.log for this.currentImage shows the original file path.
I can not for the life of me figure out yet why but am new to the web side with FileReader, Angular etc. So hopefully somebody can serve me a quick slice of humble pie?? :)
PS - If it matters, it's angular 5