Skip to content

Commit a67f231

Browse files
committed
feat(router): add support for hash-based location
Closes #2555
1 parent 0a51ccb commit a67f231

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

modules/angular2/router.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export {RouterLink} from './src/router/router_link';
1212
export {RouteParams} from './src/router/instruction';
1313
export {RouteRegistry} from './src/router/route_registry';
1414
export {LocationStrategy} from './src/router/location_strategy';
15+
export {HashLocationStrategy} from './src/router/hash_location_strategy';
1516
export {HTML5LocationStrategy} from './src/router/html5_location_strategy';
1617
export {Location, appBaseHrefToken} from './src/router/location';
1718
export {Pipeline} from './src/router/pipeline';
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import {DOM} from 'angular2/src/dom/dom_adapter';
2+
import {Injectable} from 'angular2/di';
3+
import {LocationStrategy} from './location_strategy';
4+
import {EventListener, History, Location} from 'angular2/src/facade/browser';
5+
6+
@Injectable()
7+
export class HashLocationStrategy extends LocationStrategy {
8+
private _location: Location;
9+
private _history: History;
10+
11+
constructor() {
12+
super();
13+
this._location = DOM.getLocation();
14+
this._history = DOM.getHistory();
15+
}
16+
17+
onPopState(fn: EventListener): void {
18+
DOM.getGlobalEventTarget('window').addEventListener('popstate', fn, false);
19+
}
20+
21+
getBaseHref(): string { return ''; }
22+
23+
path(): string { return this._location.hash; }
24+
25+
pushState(state: any, title: string, url: string) {
26+
this._history.pushState(state, title, '#' + url);
27+
}
28+
29+
forward(): void { this._history.forward(); }
30+
31+
back(): void { this._history.back(); }
32+
}

0 commit comments

Comments
 (0)