chartJS中的'this'(以角形表示)

hxzsmxv2  于 2022-11-06  发布在  Chart.js
关注(0)|答案(2)|浏览(146)

这是我在angular中的chartJS onclick事件处理程序:

legend: {
          onClick: this.toggleLegendClickHandler

从这个方法中,我需要在y刻度标题文本改变后更新一个图表。我想这样做,比如this._chart.chart.update(),其中_chart是childviewAngular 指令。
但是,this不是组件对象,而是Legend对象,它接收了一个事件。我可以在该处理程序中将this作为Angular 组件对象吗?

agyaoht7

agyaoht71#

使用箭头函数定义toggleLegendClickHandler函数它将指向组件对象

export class ChartComponent{

    legend: {onClick: this.toggleLegendClickHandler}

    toggleLegendClickHandler = ()=>{
    ...
    }
}
sycxhyv7

sycxhyv72#

可以使用this shadow,假设这是您的代码,

chartFunction() {
     const _that = this;  // creating shadow of this

    createChart({
    data: {
            legend: {
              onClick: _that.toggleLegendClickHandler
            }
    }
    });

}

现在它将引用组件类引用。

相关问题