Skip to content

Commit

Permalink
feat(router): add support for hash-based location
Browse files Browse the repository at this point in the history
Closes #2555
  • Loading branch information
btford committed Jun 23, 2015
1 parent 0a51ccb commit a67f231
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
1 change: 1 addition & 0 deletions modules/angular2/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export {RouterLink} from './src/router/router_link';
export {RouteParams} from './src/router/instruction';
export {RouteRegistry} from './src/router/route_registry';
export {LocationStrategy} from './src/router/location_strategy';
export {HashLocationStrategy} from './src/router/hash_location_strategy';
export {HTML5LocationStrategy} from './src/router/html5_location_strategy';
export {Location, appBaseHrefToken} from './src/router/location';
export {Pipeline} from './src/router/pipeline';
Expand Down
32 changes: 32 additions & 0 deletions modules/angular2/src/router/hash_location_strategy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import {DOM} from 'angular2/src/dom/dom_adapter';
import {Injectable} from 'angular2/di';
import {LocationStrategy} from './location_strategy';
import {EventListener, History, Location} from 'angular2/src/facade/browser';

@Injectable()
export class HashLocationStrategy extends LocationStrategy {
private _location: Location;
private _history: History;

constructor() {
super();
this._location = DOM.getLocation();
this._history = DOM.getHistory();
}

onPopState(fn: EventListener): void {
DOM.getGlobalEventTarget('window').addEventListener('popstate', fn, false);
}

getBaseHref(): string { return ''; }

path(): string { return this._location.hash; }

pushState(state: any, title: string, url: string) {
this._history.pushState(state, title, '#' + url);
}

forward(): void { this._history.forward(); }

back(): void { this._history.back(); }
}

5 comments on commit a67f231

@rolandjitsu
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@btford how do I enable hash based location?

@dsebastien
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm also interested; I didn't dare to ask given how fresh the commit was.. :D

@tuurbo
Copy link
Contributor

@tuurbo tuurbo commented on a67f231 Jun 27, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rolandjitsu @dsebastien

import {routerInjectables, LocationStrategy, HashLocationStrategy} from 'angular2/router';

bootstrap(App, [
    routerInjectables,
    bind(LocationStrategy).toClass(HashLocationStrategy)
])

@gutenye
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tuurbo

How can I import bind function?

I used to use import {bind} from "angular2/angular2" in 2.0.0-alpha.31, but as now 2.0.0-alpha.35, bind is undefined

@tuurbo
Copy link
Contributor

@tuurbo tuurbo commented on a67f231 Aug 26, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gutenye As of alpha 35 import {bind} from 'angular2/di'; works for me.

https://gitter.im/angular/angular is a good place to ask questions if your looking for more help

Please sign in to comment.