Angular 2 中的 Amcharts 单击事件不起作用 [英] Amcharts click event in Angular 2 not working

查看:23
本文介绍了Angular 2 中的 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 时出错,对吗?

Now onclick event myfunc is getting called . But strangely I cant access any global variable there using this. If its a scope issue it should give error while calling myfunc also right?

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));

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

Depending on your inputs this will also work:

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

这是以下的较短版本:

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

建议阅读:如何访问正确的`this`回调中的上下文?

这篇关于Angular 2 中的 Amcharts 单击事件不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆