javascript - Angular 2 中的 Amcharts 点击事件不起作用

标签 javascript angular typescript amcharts

这是我的代码库

        export class myComponent implements OnInit {
          minHist;
          maxHist;
        }

        public callAmcharts(whichFilterType:String){

                    this.amchart = AmCharts.makeChart( "chart", {
                      "type": "serial",
                      "theme": "light",
                      "dataProvider": this.TrendData,
                      "valueAxes": [ {
                        "gridColor": "#FFFFFF",
                        "gridAlpha": 0.2,
                        "dashLength": 0
                      }],
                      "gridAboveGraphs": true,
                      "startDuration": 1,
                      "graphs": [ {
                        "balloonText": "[[category]]: <b>[[value]]</b>",
                        "fillAlphas": 0.8,
                        "lineAlpha": 0.2,
                        "type": "column",
                        "valueField": "dCount",
                        "showHandOnHover":true
                      } ],

                      "chartCursor": {
                        "categoryBalloonEnabled": false,
                        "cursorAlpha": 0,
                        "zoomable": false
                      },
                      "categoryField": "dRange",
                      "categoryAxis": {
                        "gridPosition": "start",
                        "gridAlpha": 0,
                        "tickPosition": "start",
                        "tickLength": 20,
                        "labelRotation": 45
                      },
                      "export": {
                        "enabled": true
                      }

                    });

   this.amchart.addListener("clickGraphItem",this.myfunc);

}

现在 onclick 事件 myfunc 正在被调用。但奇怪的是,我无法使用 this 访问那里的任何全局变量。如果它是一个 scope 问题,那么在调用 myfunc 时应该会出错,对吗?

public myfunc(e:any){
        var range= e.item.category;
        let range_arr = range.split("-");
        this.minHist=range_arr[0];
        this.maxHist=range_arr[1];

    } 

我收到错误消息 Uncaught TypeError: Cannot set property 'minHist' of undefined

最佳答案

改变

this.amchart.addListener("clickGraphItem",this.myfunc);

this.amchart.addListener("clickGraphItem",this.myfunc.bind(this));

根据您的输入,这也将起作用:

this.amchart.addListener("clickGraphItem",(e)=> this.myfunc(e));

这是以下内容的较短版本:

this.amchart.addListener("clickGraphItem",(e)=> { return this.myfunc(e); });

建议阅读:How to access the correct `this` context inside a callback?

关于javascript - Angular 2 中的 Amcharts 点击事件不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43447013/

相关文章:

javascript - 如何指定文件夹内的所有 typescript 文件作为 grunt 的源?

reactjs - TS2339 : Property X does not exist on type '{}'

javascript - 在 Laravel/Vue.js 项目中共享静态/配置数据的最佳方式是什么

javascript - 如何从 gulpfile 获取变量到前端脚本?

javascript - d3 瀑布图在 x 轴值上旋转 180 到 -90 度

angular - 在路由中将对象作为参数传递返回 [Object object]

javascript - 如何从使用 ngFor 指令动态添加的 HTML 表中恢复标题单元格数据

java - 如何在 TypeScript 中定义结构?

javascript - 使用多个版本格式时的情况

angular - 无法绑定(bind)到 'x',因为它不是 'y' 的已知属性