【问题标题】:Amcharts click event in Angular 2 not workingAngular 2中的Amcharts单击事件不起作用
【发布时间】:2017-04-17 07:18:40
【问题描述】:

这是我的代码库

        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时出错

【问题讨论】:

    标签: javascript angular typescript amcharts


    【解决方案1】:

    改变

    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?

    【讨论】:

    • 是的,这是一篇不错的文章。感谢您的回答,它也有效。
    猜你喜欢
    • 2017-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-04
    相关资源
    最近更新 更多