当用户点击一个数据点时,我们需要显示一个模态窗口。
我们拥有的代码是:
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);
}
我们可以在控制台中看到日志数据。但不是模态。我们该如何解决这个问题呢?
发布于 2019-04-17 03:37:16
代码看起来很好,但是正如@yurzui提到的,问题出在这里
bullet.events.on("hit", function (ev) {}
因为它将从另一个上下文中调用,所以modalService在那里不可用。尝试使用箭头函数,以便this context is preserved.To修复此问题,
bullet.events.on("hit", (ev) => {
console.log(ev.target._dataItem.dataContext);
this.modalService.open(this.dialog);
});
https://stackoverflow.com/questions/55715247
复制相似问题