After creating a custom LocationStrategy
to disable browser location bar changes, I am now looking to integrate smaller apps into various web pages without affecting the browser's location. While navigation works smoothly with this new strategy, I am facing a challenge in implementing a back()
-function for navigation. Calling window.back()
is no longer an option and there seems to be no internal method within Angular sources that can achieve this. Perhaps directly calling LocationStrategy.back()
could work, but then I need a way to update the router for the current view. Is there an event that can be triggered or another solution to refresh the view?
Below is the current implementation of my custom location strategy:
import { Injectable, Inject, Optional, platform } from 'angular2/core';
import { LocationStrategy, PlatformLocation, APP_BASE_HREF, } from 'angular2/router';
import { joinWithSlash, normalizeQueryParams } from 'angular2/src/router/location_strategy';
import { UrlChangeListener } from 'angular2/src/router/location/platform_location';
import { isPresent } from 'angular2/src/facade/lang';
@Injectable()
export class HiddenLocationStrategy extends LocationStrategy {
private _baseHref: string = '';
private pathHistory: string[] = [];
private poppedPathHistory: string[] = [];
constructor(private _platformLocation: PlatformLocation,
@Optional() @Inject(APP_BASE_HREF) _baseHref?: string) {
super();
if (isPresent(_baseHref)) {
this._baseHref = _baseHref;
}
}
onPopState(fn: UrlChangeListener): void {
}
getBaseHref(): string { return this._baseHref }
path(): string {
return this.pathHistory.length > 0 ? this.pathHistory[this.pathHistory.length - 1] : '';
}
prepareExternalUrl(internal: string): string {
var url = joinWithSlash(this._baseHref, internal);
return url;
}
pushState(state: any, title: string, path: string, queryParams: string) {
this.pathHistory.push(path);
}
replaceState(state: any, title: string, path: string, queryParams: string) {
}
forward(): void { this.pathHistory.push(this.poppedPathHistory.pop()); }
back(): void { this.poppedPathHistory.push(this.pathHistory.pop()); }
}