Calliste Hanriat Calliste Hanriat - 1 month ago 13
TypeScript Question

call another function from onreadystatechange Angular 2

How can I call one of my Component function from

onreadystatechange
(because it does not recognize
this
as my class but as
xmlhttpelement
)

xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4) {
if (xmlhttp.status == 200) {
this.anotherFunction(xmlhttp); //this is considered as xmlhttp instead of my class which contains this function
}
}

Answer

Use arrow function instead:

xmlhttp.onreadystatechange = () => {
...
}

This way this will reference to component instance

Comments