I am writing a TypeScript browser application. I need to read the contents of an XML file, and store it in a class. I am having issues with the 'this' object. Here is what I have so far:
class FileTools {
text: string;
readSingleFile(e) {
var fileName = e.target.files[0];
if (!fileName) {
return;
}
var reader = new FileReader();
reader.onload = file => {
var contents: any = file.target;
this.text = contents.result; <== Issue here. 'this' is the filereader
};
reader.readAsText(fileName);
}
start() {
document.getElementById("file-input").addEventListener("change", this.readSingleFile, false);
}
}
window.onload = () => {
var tcx = new FileTools();
tcx.start();
};
The HTML has a file selection box input type="file" id="file-input"
The issue is that when the file is loaded, using the 'this' points to the file reader, not to my class. If I add a 'self' variable first like this:
readSingleFile(e) {
var fileName = e.target.files[0];
if (!fileName) {
return;
}
var reader = new FileReader();
var self = this;
reader.onload = file => {
var contents: any = file.target;
self.text = contents.result; <== Issue here. 'this' is the filereader
};
reader.readAsText(fileName);
}
then self points to the input box (as that is the context of the outer method).
So the question is, how do I get the real 'this' object for the FileTools reference.
Thanks.