I'm trying to dismiss a full-window modal when the browser back button is pressed with this code:
class MyModalComponent {
constructor(private _location: LocationStrategy) { }
public ngOnInit(): void {
this._location.pushState(this.url, this.title, this.url, '');
}
@HostListener('window:popstate', ['$event'])
public onPopState(event: PopStateEvent) {
this.modal.dismiss();
event.preventDefault();
event.stopPropagation();
event.cancelBubble = true;
}
}
When the back button is pressed, onPopState is invoked and the modal is dismissed, but also Angular Router detects the URL change and starts navigating to the previous state. Is there any way to cancel the navigation or prevent Angular Router listening to URL changes when a modal is open?