我们需要在用户单击数据点时显示一个模态窗口。
我们拥有的代码是:
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 上下文。要解决这个问题,
bullet.events.on("hit", (ev) => {
console.log(ev.target._dataItem.dataContext);
this.modalService.open(this.dialog);
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句