My Angular2 app uses a ng-bootstrap modal to show some result charts in detail. For this reason i resized the modal to nearly fullscreen (only margin: 20px
left). This causes some users to use the browser back button instead of the close button on the top right or bottom of the page.
What i'm trying now is to cancel the default browser back event and close the modal instead when the event is called.
I'm using some code from here as code base to listen to the browser event and extended it with some stuff:
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
The problem is that i do not know how and whether i can cancel the default back event.
location.onPopState((event) => {
event.preventDefault();
});
This actually does not work. Same for this solution. Maybe i can insert some "fake" history stack when open up the modal ?!
For AngularJS 1.x it seems actually working: https://stackoverflow.com/a/33454993/3623608