我是Angular 2的新手。我在Angular中编写了以下代码
export class TestClass {
constructor() {
this.initMap();
}
initMap() {
this.marker.addListener('dragend', this.onMarkerDrop);
}
onMarkerDrop(event) {
this.functionTwo(); // Getting error this.functionTwo is not a function
}
functionTwo() {
}
}
注意:在问这个问题之前,我在stackoverflow中搜索了这些链接
他们说使用Arrow函数来调用其他成员函数。但是我不知道如何在我的代码中实现他们的建议。可能我对它们的理解不正确。
我想要您的帮助,如何使用functionOne();中的箭头函数调用this.functionTwo();
谢谢您的帮助。
您的函数onMarkerDrop
将作为回调传递,其中上下文将更改并且this
将具有不同的值。发送时使用箭头或绑定以保留上下文。
this.marker.addListener('dragend', this.onMarkerDrop.bind(this));
要么
this.marker.addListener('dragend', ($event)=>this.onMarkerDrop($event));
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句