JavaScript How to set a Component's property value from inside a FileReader event component definition,component to hdmi,component synonym,component form,components

I have this component:

export class FileFormComponent {
  _base64: string;

 handleFile($event){
   var input = $event.target;
   var reader = new FileReader();
   reader.onloadend = function() {
     this._base64 = reader.result
   }
   reader.readAsDataURL(input.files[0]);
   console.log(this._base64);
 }
}

The problem is the console.log(this._base64) logs undefined. It seems this inside the onloadend event does not refer to the component's context. how can I set the value of the _base64 property from inside the FileReader onloadend event.

Answer:1

I would use an arrow function. This way you could use the lexical this (that corresponds to the component instance):

reader.onloadend() = () => {
  this._base64 = reader.result
}

See this link for more details:

Answer:2

After add some conditional class and attribute binding in the template, angular keep those attribute directive in the dom when I check in the dev tool like this <input type="radio" name="gender" ...

After add some conditional class and attribute binding in the template, angular keep those attribute directive in the dom when I check in the dev tool like this <input type="radio" name="gender" ...

  1. prevent rendering angular

I like to generate a coverage report for my typescript source files with karma-coverage. My unit tests are written in javascript and I'm using the Jasmine Test framework. My folder structure looks ...

I like to generate a coverage report for my typescript source files with karma-coverage. My unit tests are written in javascript and I'm using the Jasmine Test framework. My folder structure looks ...

I am trying to find all OPTION inside the SELECT using following JavaScript code. var temp = document.querySelectorAll("select"); console.log(temp[0].childNodes); and html markup is this <...

I am trying to find all OPTION inside the SELECT using following JavaScript code. var temp = document.querySelectorAll("select"); console.log(temp[0].childNodes); and html markup is this <...

  1. what is select text

So I've looked though some of the questions already asked about this issue and have not been able to find a solution that works for me. I am using Bootstrap (v 3.3.5) to create a navbar menu at the ...

So I've looked though some of the questions already asked about this issue and have not been able to find a solution that works for me. I am using Bootstrap (v 3.3.5) to create a navbar menu at the ...