从onreadystatechange Angular 2调用另一个函数

时间:2016-10-06 18:03:34

标签: angular typescript

如何从onreadystatechange调用我的某个组件功能(因为它不能将this识别为我的班级,而是识别为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
        }
    }

1 个答案:

答案 0 :(得分:6)

改为使用箭头功能:

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

这种方式this将引用组件实例

相关问题