angular - Can't reference this in typescript -


i have fileupload in angular2 app, sends file bytearray webservice. use file reader create bytearray, has onload-event. in event can't reference uploadservice using this.uploadservice.

public uploadhandler(event) {     var reader = new filereader();     reader.onload = function() {         var arraybuffer = this.result, array = new uint8array(arraybuffer), binarystring = string.fromcharcode.apply(null, array);         //this.uploadservice.doupload(binarystring);     }     reader.readasarraybuffer(event.files[0]); } 

i tried use fat arrow, can't reference result anymore.

reader.onload = () => {     //this.result not exist anymore!     var arraybuffer = result, array = new uint8array(arraybuffer), binarystring = string.fromcharcode.apply(null, array);     this.uploadservice.doupload(binarystring); } 

the fat arrow right way go. , can result reader variable instead of this. this

public uploadhandler(event) {     var reader = new filereader();     reader.onload = () => {         var arraybuffer = reader.result;         var array = new uint8array(arraybuffer);         var binarystring = string.fromcharcode.apply(null, array);         this.uploadservice.doupload(binarystring);     };     reader.readasarraybuffer(event.files[0]); } 

Comments