【发布时间】:2018-07-27 00:37:19
【问题描述】:
我是 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 中搜索了这些链接
'this.function' is not a function - OO JS
this.function is not a function :typescript
Angular - this.function is not a function
他们说使用箭头函数来调用其他成员函数。 但我不知道如何在我的代码中实施他们的建议。可能我没有正确理解它们。
我需要你的帮助,如何使用 functionOne() 中的箭头函数调用 this.functionTwo();
感谢您的帮助。
【问题讨论】:
-
这将 100% 正常工作,应该有比这段代码更多的东西
-
这么多代码肯定可以工作。提供更多细节。
-
您将函数作为回调发送.. 使用绑定。
this.marker.addListener('dragend', this.onMarkerDrop.bind(this)); -
您好 VivekDoshi 和 Arun Raj R,感谢您的回复。我已经更新了我的代码,请检查一次。实际上在 onMarkerDrop() 上的函数中我得到了那个错误。
标签: javascript angular google-maps typescript ionic-framework