fix(router): preserve fragment on initial load

This commit is contained in:
vsavkin 2016-06-25 11:51:15 -07:00
parent a620f95891
commit 90295e3252
6 changed files with 15 additions and 8 deletions

View File

@ -77,7 +77,7 @@ export class HashLocationStrategy extends LocationStrategy {
getBaseHref(): string { return this._baseHref; } getBaseHref(): string { return this._baseHref; }
path(): string { path(includeHash: boolean = false): string {
// the hash value is always prefixed with a `#` // the hash value is always prefixed with a `#`
// and if it is empty then it will stay empty // and if it is empty then it will stay empty
var path = this._platformLocation.hash; var path = this._platformLocation.hash;

View File

@ -70,14 +70,19 @@ export class Location {
var browserBaseHref = this._platformStrategy.getBaseHref(); var browserBaseHref = this._platformStrategy.getBaseHref();
this._baseHref = Location.stripTrailingSlash(_stripIndexHtml(browserBaseHref)); this._baseHref = Location.stripTrailingSlash(_stripIndexHtml(browserBaseHref));
this._platformStrategy.onPopState((ev) => { this._platformStrategy.onPopState((ev) => {
ObservableWrapper.callEmit(this._subject, {'url': this.path(), 'pop': true, 'type': ev.type}); ObservableWrapper.callEmit(
this._subject, {'url': this.path(true), 'pop': true, 'type': ev.type});
}); });
} }
/** /**
* Returns the normalized URL path. * Returns the normalized URL path.
*/ */
path(): string { return this.normalize(this._platformStrategy.path()); } // TODO: vsavkin. Remove the boolean flag and always include hash once the deprecated router is
// removed.
path(includeHash: boolean = false): string {
return this.normalize(this._platformStrategy.path(includeHash));
}
/** /**
* Normalizes the given path and compares to the current normalized path. * Normalizes the given path and compares to the current normalized path.

View File

@ -28,7 +28,7 @@ import {UrlChangeListener} from './platform_location';
* @stable * @stable
*/ */
export abstract class LocationStrategy { export abstract class LocationStrategy {
abstract path(): string; abstract path(includeHash: boolean): string;
abstract prepareExternalUrl(internal: string): string; abstract prepareExternalUrl(internal: string): string;
abstract pushState(state: any, title: string, url: string, queryParams: string): void; abstract pushState(state: any, title: string, url: string, queryParams: string): void;
abstract replaceState(state: any, title: string, url: string, queryParams: string): void; abstract replaceState(state: any, title: string, url: string, queryParams: string): void;

View File

@ -98,9 +98,11 @@ export class PathLocationStrategy extends LocationStrategy {
return Location.joinWithSlash(this._baseHref, internal); return Location.joinWithSlash(this._baseHref, internal);
} }
path(): string { path(includeHash: boolean = false): string {
return this._platformLocation.pathname + const pathname = this._platformLocation.pathname +
Location.normalizeQueryParams(this._platformLocation.search); Location.normalizeQueryParams(this._platformLocation.search);
const hash = this._platformLocation.hash;
return hash && includeHash ? `${pathname}${hash}` : pathname;
} }
pushState(state: any, title: string, url: string, queryParams: string) { pushState(state: any, title: string, url: string, queryParams: string) {

View File

@ -32,7 +32,7 @@ export class MockLocationStrategy extends LocationStrategy {
ObservableWrapper.callEmit(this._subject, new _MockPopStateEvent(this.path())); ObservableWrapper.callEmit(this._subject, new _MockPopStateEvent(this.path()));
} }
path(): string { return this.internalPath; } path(includeHash: boolean = false): string { return this.internalPath; }
prepareExternalUrl(internal: string): string { prepareExternalUrl(internal: string): string {
if (internal.startsWith('/') && this.internalBaseHref.endsWith('/')) { if (internal.startsWith('/') && this.internalBaseHref.endsWith('/')) {

View File

@ -123,7 +123,7 @@ export class Router {
*/ */
initialNavigation(): void { initialNavigation(): void {
this.setUpLocationChangeListener(); this.setUpLocationChangeListener();
this.navigateByUrl(this.location.path()); this.navigateByUrl(this.location.path(true));
} }
/** /**