D3.js和 Angular 'this' 元素冲突



使用D3.js在我的Angular 6项目中,我正在选择一个元素并在其上添加一些文本。在此之后,我想调用我的 typeScript 函数,但this引用的是 D3.js 元素。

下面是一个示例代码:

MyVisulizationFuncn(){
d3.select("p").text("Thanks for your time to solve this").on("click", this.myFunciton());
}
myFunciton(){
console.log("I finally get called");
}

有人可以建议如何在点击事件 d3 中调用我的同一 component.ts 文件中定义的函数吗?

this赋值给变量并通过变量调用函数。

MyVisulizationFuncn(){
let th = this;
d3.select("p").text("Thanks for your time to solve this").on("click", th.myFunciton());
}

最新更新