【发布时间】:2017-07-28 02:01:30
【问题描述】:
我的 Angular2 应用程序使用ng-bootstrap modal 来详细显示一些结果图表。出于这个原因,我将模式调整为几乎全屏(仅margin: 20px 左)。这会导致一些用户使用浏览器的后退按钮,而不是页面右上角或底部的关闭按钮。
我现在正在尝试的是取消默认的浏览器返回事件并在调用事件时关闭模式。
我正在使用来自here 的一些代码作为代码库来监听浏览器事件并用一些东西对其进行扩展:
import { PlatformLocation } from '@angular/common'
(...)
modalRef: NgbModalRef;
constructor(location: PlatformLocation) {
location.onPopState(() => {
console.log('pressed back!');
// example for a simple check if modal is opened
if(this.modalRef !== undefined)
{
console.log('modal is opened - cancel default browser event and close modal');
// TODO: cancel the default event
// close modal
this.modalRef.close();
}
else
{
console.log('modal is not opened - default browser event');
}
});
}
(...)
// some code to open the modal and store the reference to this.modalRef
问题是我不知道如何以及是否可以取消默认的返回事件。
location.onPopState((event) => {
event.preventDefault();
});
这实际上是行不通的。 this solution 也一样。 也许我可以在打开模式时插入一些“假”历史堆栈?!
对于 AngularJS 1.x,它似乎确实有效:https://stackoverflow.com/a/33454993/3623608
【问题讨论】:
标签: angular ng-bootstrap