AmCharts XY 图表 - 如何在 Angular 中点击时获得模态弹出窗口?

AmCharts XY Chart - How to get modal popup on click in Angular?

我们需要在用户单击数据点时显示模式 window。

我们的代码是:

  constructor(public dataservice: DataserviceService, private modalService: NgbModal, private router: Router) { }
  ...
  ...
  bullet.events.on("hit", function (ev) {
    console.log(ev.target._dataItem.dataContext);
    this.modalService.open(this.dialog);
  });
}

public showDialog() {
  this.modalService.open(this.dialog);
}

我们能够在控制台中看到日志数据......但不是模态。我们如何解决这个问题?

代码看起来不错,但是正如@yurzui 提到的,问题出在这里

bullet.events.on("hit", function (ev) {}

因为它将从另一个上下文调用,所以 modalService 在那里不可用。尝试使用箭头函数,使 this 上下文为 preserved.To fix this,

bullet.events.on("hit", (ev) => {
    console.log(ev.target._dataItem.dataContext);
    this.modalService.open(this.dialog);
 });