Skip to content

Commit 4e24633

Browse files
Fix adding the hash to search and path
1 parent 8f51d29 commit 4e24633

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

src/location/locationService.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,21 @@ export class Ng2LocationServices extends BaseLocationServices {
1919

2020
_set(state: any, title: string, url: string, replace: boolean): any {
2121
const { path, search, hash } = parseUrl(url);
22-
const urlPart = search ? path : path + (hash ? '#' + hash : '');
23-
const searchPart = search + (hash ? '#' + hash : '');
22+
23+
const hashWithPrefix = hash ? '#' + hash : '';
24+
let urlPath = path;
25+
let urlParams = search;
26+
27+
if (search) {
28+
urlParams += hashWithPrefix;
29+
} else {
30+
urlPath += hashWithPrefix;
31+
}
2432

2533
if (replace) {
26-
this._locationStrategy.replaceState(state, title, urlPart, searchPart);
34+
this._locationStrategy.replaceState(state, title, urlPath, urlParams);
2735
} else {
28-
this._locationStrategy.pushState(state, title, urlPart, searchPart);
36+
this._locationStrategy.pushState(state, title, urlPath, urlParams);
2937
}
3038
}
3139

test/location/locationService.spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ describe('locationService', () => {
4444
expectUrlReadAfterWrite(HashLocationStrategy, '/foo');
4545
});
4646

47+
it('should read/write the url path with single hash', () => {
48+
expectUrlReadAfterWrite(HashLocationStrategy, '/foo#test');
49+
});
50+
4751
it('should read/write query params', () => {
4852
expectUrlReadAfterWrite(HashLocationStrategy, '/foo?query1=value1');
4953
});
@@ -66,6 +70,10 @@ describe('locationService', () => {
6670
expectUrlReadAfterWrite(PathLocationStrategy, '/foo');
6771
});
6872

73+
it('should read/write the url path with single hash', () => {
74+
expectUrlReadAfterWrite(HashLocationStrategy, '/foo#test');
75+
});
76+
6977
it('should read/write query params', () => {
7078
expectUrlReadAfterWrite(PathLocationStrategy, '/foo?query1=value1');
7179
});

0 commit comments

Comments
 (0)