X Tutup
Skip to content

Commit 53bddec

Browse files
committed
fix(router): respond to hashchange events
Previously if the URL changed in `HashLocation` mode, the router would not pick up the change. This adds a listener in `HashLocationStrategy` for `hashchange` events to fix the problem. Closes #5013
1 parent cb2b690 commit 53bddec

File tree

6 files changed

+95
-0
lines changed

6 files changed

+95
-0
lines changed

modules/angular2/src/router/path_location_strategy.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ export class PathLocationStrategy extends LocationStrategy {
6363

6464
onPopState(fn: EventListener): void {
6565
DOM.getGlobalEventTarget('window').addEventListener('popstate', fn, false);
66+
DOM.getGlobalEventTarget('window').addEventListener('hashchange', fn, false);
6667
}
6768

6869
getBaseHref(): string { return this._baseHref; }
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
library playground.e2e_test.hash_location_spec;
2+
3+
main() {}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import {verifyNoBrowserErrors} from 'angular2/src/testing/e2e_util';
2+
import {Promise} from 'angular2/src/core/facade/async';
3+
4+
function waitForElement(selector) {
5+
var EC = (<any>protractor).ExpectedConditions;
6+
// Waits for the element with id 'abc' to be present on the dom.
7+
browser.wait(EC.presenceOf($(selector)), 20000);
8+
}
9+
10+
describe('hash routing example app', function() {
11+
afterEach(verifyNoBrowserErrors);
12+
13+
var URL = 'playground/src/hash_routing/index.html';
14+
15+
it('should navigate between routes', function() {
16+
browser.get(URL + '#/bye');
17+
waitForElement('goodbye-cmp');
18+
19+
element(by.css('#hello-link')).click();
20+
waitForElement('hello-cmp');
21+
22+
expect(element(by.css('hello-cmp')).getText()).toContain('hello');
23+
24+
browser.navigate().back();
25+
waitForElement('goodbye-cmp');
26+
27+
expect(element(by.css('goodbye-cmp')).getText()).toContain('goodbye');
28+
});
29+
});
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!doctype html>
2+
<html>
3+
<title>Routing Example</title>
4+
<base href="/playground/src/hash_routing/">
5+
<body>
6+
<example-app>
7+
Loading...
8+
</example-app>
9+
10+
$SCRIPTS$
11+
</body>
12+
</html>
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import {Component, provide} from 'angular2/angular2';
2+
import {bootstrap} from 'angular2/bootstrap';
3+
import {
4+
RouteConfig,
5+
Route,
6+
ROUTER_PROVIDERS,
7+
ROUTER_DIRECTIVES,
8+
HashLocationStrategy,
9+
LocationStrategy
10+
} from 'angular2/router';
11+
12+
import {reflector} from 'angular2/src/core/reflection/reflection';
13+
import {ReflectionCapabilities} from 'angular2/src/core/reflection/reflection_capabilities';
14+
15+
@Component({selector: 'hello-cmp', template: `hello`})
16+
class HelloCmp {
17+
}
18+
19+
20+
@Component({selector: 'goodbye-cmp', template: `goodbye`})
21+
class GoodByeCmp {
22+
}
23+
24+
25+
@Component({
26+
selector: 'example-app',
27+
template: `
28+
<h1>My App</h1>
29+
<nav>
30+
<a href="#/" id="hello-link">Navigate via href</a> |
31+
<a [router-link]="['/GoodbyeCmp']" id="goodbye-link">Navigate with Link DSL</a>
32+
</nav>
33+
<router-outlet></router-outlet>
34+
`,
35+
directives: [ROUTER_DIRECTIVES]
36+
})
37+
@RouteConfig([
38+
new Route({path: '/', component: HelloCmp, name: 'HelloCmp'}),
39+
new Route({path: '/bye', component: GoodByeCmp, name: 'GoodbyeCmp'})
40+
])
41+
class AppCmp {
42+
}
43+
44+
45+
export function main() {
46+
reflector.reflectionCapabilities = new ReflectionCapabilities();
47+
bootstrap(AppCmp,
48+
[ROUTER_PROVIDERS, provide(LocationStrategy, {useClass: HashLocationStrategy})]);
49+
}

tools/broccoli/trees/browser_tree.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ const kServedPaths = [
4343
'playground/src/person_management',
4444
'playground/src/order_management',
4545
'playground/src/gestures',
46+
'playground/src/hash_routing',
4647
'playground/src/hello_world',
4748
'playground/src/http',
4849
'playground/src/jsonp',

0 commit comments

Comments
 (0)
X Tutup