0

当用户单击数据点时,我们需要显示模式窗口。

我们的代码是:

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

在此处输入图像描述 我们能够在控制台中看到日志数据.. 但不是模式。我们如何解决这个问题?

4

1 回答 1

1

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

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

由于它将从另一个上下文中调用,因此 modalService 在那里不可用。尝试使用箭头函数,以便保留 this 上下文。要解决此问题,

bullet.events.on("hit", (ev) => {
    console.log(ev.target._dataItem.dataContext);
    this.modalService.open(this.dialog);
 });
于 2019-04-16T19:37:15.840 回答