From 90295e3252b627986718fcc92890ae6a31ac6c3e Mon Sep 17 00:00:00 2001 From: vsavkin Date: Sat, 25 Jun 2016 11:51:15 -0700 Subject: [PATCH] fix(router): preserve fragment on initial load --- .../common/src/location/hash_location_strategy.ts | 2 +- modules/@angular/common/src/location/location.ts | 9 +++++++-- .../@angular/common/src/location/location_strategy.ts | 2 +- .../common/src/location/path_location_strategy.ts | 6 ++++-- .../@angular/common/testing/mock_location_strategy.ts | 2 +- modules/@angular/router/src/router.ts | 2 +- 6 files changed, 15 insertions(+), 8 deletions(-) diff --git a/modules/@angular/common/src/location/hash_location_strategy.ts b/modules/@angular/common/src/location/hash_location_strategy.ts index fe6892a648..8677309802 100644 --- a/modules/@angular/common/src/location/hash_location_strategy.ts +++ b/modules/@angular/common/src/location/hash_location_strategy.ts @@ -77,7 +77,7 @@ export class HashLocationStrategy extends LocationStrategy { getBaseHref(): string { return this._baseHref; } - path(): string { + path(includeHash: boolean = false): string { // the hash value is always prefixed with a `#` // and if it is empty then it will stay empty var path = this._platformLocation.hash; diff --git a/modules/@angular/common/src/location/location.ts b/modules/@angular/common/src/location/location.ts index ebd38bbd00..f19b41b7a7 100644 --- a/modules/@angular/common/src/location/location.ts +++ b/modules/@angular/common/src/location/location.ts @@ -70,14 +70,19 @@ export class Location { var browserBaseHref = this._platformStrategy.getBaseHref(); this._baseHref = Location.stripTrailingSlash(_stripIndexHtml(browserBaseHref)); 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. */ - 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. diff --git a/modules/@angular/common/src/location/location_strategy.ts b/modules/@angular/common/src/location/location_strategy.ts index 6d63c57781..5e8bd79f82 100644 --- a/modules/@angular/common/src/location/location_strategy.ts +++ b/modules/@angular/common/src/location/location_strategy.ts @@ -28,7 +28,7 @@ import {UrlChangeListener} from './platform_location'; * @stable */ export abstract class LocationStrategy { - abstract path(): string; + abstract path(includeHash: boolean): string; abstract prepareExternalUrl(internal: string): string; abstract pushState(state: any, title: string, url: string, queryParams: string): void; abstract replaceState(state: any, title: string, url: string, queryParams: string): void; diff --git a/modules/@angular/common/src/location/path_location_strategy.ts b/modules/@angular/common/src/location/path_location_strategy.ts index 114e983034..2670d54f05 100644 --- a/modules/@angular/common/src/location/path_location_strategy.ts +++ b/modules/@angular/common/src/location/path_location_strategy.ts @@ -98,9 +98,11 @@ export class PathLocationStrategy extends LocationStrategy { return Location.joinWithSlash(this._baseHref, internal); } - path(): string { - return this._platformLocation.pathname + + path(includeHash: boolean = false): string { + const pathname = this._platformLocation.pathname + 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) { diff --git a/modules/@angular/common/testing/mock_location_strategy.ts b/modules/@angular/common/testing/mock_location_strategy.ts index 059577c79e..88960b79ee 100644 --- a/modules/@angular/common/testing/mock_location_strategy.ts +++ b/modules/@angular/common/testing/mock_location_strategy.ts @@ -32,7 +32,7 @@ export class MockLocationStrategy extends LocationStrategy { 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 { if (internal.startsWith('/') && this.internalBaseHref.endsWith('/')) { diff --git a/modules/@angular/router/src/router.ts b/modules/@angular/router/src/router.ts index b0d1e6495c..62db733054 100644 --- a/modules/@angular/router/src/router.ts +++ b/modules/@angular/router/src/router.ts @@ -123,7 +123,7 @@ export class Router { */ initialNavigation(): void { this.setUpLocationChangeListener(); - this.navigateByUrl(this.location.path()); + this.navigateByUrl(this.location.path(true)); } /**