Búsqueda de sitios web

typescript - llamar a otra función desde onreadystatechange Angular 2


¿Cómo puedo llamar a una de las funciones de mi Componente desde onreadystatechange (porque no reconoce this como mi clase sino como 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
        }
    }

Respuesta #1

Usa la función flecha en su lugar:

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

De esta manera this hará referencia a la instancia del componente