Reputation: 285
I have a service in angular which in some cases dynamically creates a html input like this:
this.fileInput = this.document.createElement('input');
this.fileInput.setAttribute('type', 'file');
this.fileInput.setAttribute('accept', 'image/*');
this.fileInput.addEventListener('change', this.onFileSelected);
this.fileInput.click();
The method onFileSelected:
async onFileSelected(event) {
const file = event.target.files[0];
const reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = (_event) => {
this.uploadFile(reader.result);
};
}
When I call it i get the following error:
this.uploadFile is not a function
I'm guessing this is because I call the function without parentheses, but I cant call it any other way. Is there any workaround for this?
Upvotes: 0
Views: 72
Reputation: 34445
Modify the way you add the handler
this.fileInput.addEventListener('change', e=> this.onFileSelected(e));
Normally, this
should now correctly reference your class (I believe that it will reference the file input dom element otherwise)
Upvotes: 1