From d20488752bc3a62f17cf695e6d8c4de9ce892f43 Mon Sep 17 00:00:00 2001 From: vsavkin Date: Sun, 26 Jun 2016 11:52:32 -0700 Subject: [PATCH] fix(router): top-levels do not work in ngIf --- .../common/src/location/location_strategy.ts | 2 +- modules/@angular/router/src/router.ts | 3 +- modules/@angular/router/test/router.spec.ts | 40 ++++++++++++++++--- modules/@angular/router/tsconfig.json | 2 + tools/public_api_guard/common/index.d.ts | 8 ++-- 5 files changed, 44 insertions(+), 11 deletions(-) diff --git a/modules/@angular/common/src/location/location_strategy.ts b/modules/@angular/common/src/location/location_strategy.ts index 5e8bd79f82..4a3f5f423b 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(includeHash: boolean): 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/router/src/router.ts b/modules/@angular/router/src/router.ts index 0429164b41..1ed6a86ad5 100644 --- a/modules/@angular/router/src/router.ts +++ b/modules/@angular/router/src/router.ts @@ -276,7 +276,7 @@ export class Router { return new Promise((resolvePromise, rejectPromise) => { let updatedUrl: UrlTree; let state: RouterState; - let navigationIsSuccessful; + let navigationIsSuccessful: boolean; applyRedirects(url, this.config) .mergeMap(u => { updatedUrl = u; @@ -494,6 +494,7 @@ class ActivateRoutes { const currRoot = this.currState ? this.currState._root : null; pushQueryParamsAndFragment(this.futureState); + advanceActivatedRoute(this.futureState.root); this.activateChildRoutes(futureRoot, currRoot, parentOutletMap); } diff --git a/modules/@angular/router/test/router.spec.ts b/modules/@angular/router/test/router.spec.ts index a394e15a85..1a559232b2 100644 --- a/modules/@angular/router/test/router.spec.ts +++ b/modules/@angular/router/test/router.spec.ts @@ -518,10 +518,28 @@ describe('Integration', () => { it('should support top-level link', fakeAsync( inject([Router, TestComponentBuilder], (router: Router, tcb: TestComponentBuilder) => { - let fixture = tcb.createFakeAsync(AbsoluteLinkCmp); + const fixture = tcb.createFakeAsync(RelativeLinkInIfCmp); advance(fixture); - expect(fixture.debugElement.nativeElement).toHaveText('link'); + router.resetConfig( + [{path: 'simple', component: SimpleCmp}, {path: '', component: BlankCmp}]); + + router.navigateByUrl('/'); + advance(fixture); + expect(fixture.debugElement.nativeElement).toHaveText(' '); + const cmp = fixture.debugElement.componentInstance; + + cmp.show = true; + advance(fixture); + + expect(fixture.debugElement.nativeElement).toHaveText('link '); + const native = fixture.debugElement.nativeElement.querySelector('a'); + + expect(native.getAttribute('href')).toEqual('/simple'); + native.click(); + advance(fixture); + + expect(fixture.debugElement.nativeElement).toHaveText('link simple'); }))); it('should support query params and fragments', @@ -731,13 +749,13 @@ describe('Integration', () => { advance(fixture); expect(location.path()).toEqual('/team/22'); - let successStatus; + let successStatus: boolean; router.navigateByUrl('/team/33').then(res => successStatus = res); advance(fixture); expect(location.path()).toEqual('/team/33'); expect(successStatus).toEqual(true); - let canceledStatus; + let canceledStatus: boolean; router.navigateByUrl('/team/44').then(res => canceledStatus = res); advance(fixture); expect(location.path()).toEqual('/team/33'); @@ -1017,6 +1035,16 @@ class DummyLinkWithParentCmp { class RelativeLinkCmp { } +@Component({ + selector: 'link-cmp', + template: + `
link
`, + directives: ROUTER_DIRECTIVES +}) +class RelativeLinkInIfCmp { + show: boolean = false; +} + @Component({ selector: 'link-cmp', template: `link`, @@ -1040,7 +1068,9 @@ class CollectParamsCmp { a.url.forEach(u => this.urls.push(u)); } - recordedUrls(): string[] { return this.urls.map(a => a.map(p => p.path).join('/')); } + recordedUrls(): string[] { + return this.urls.map((a: any) => a.map((p: any) => p.path).join('/')); + } } @Component({selector: 'blank-cmp', template: ``, directives: ROUTER_DIRECTIVES}) diff --git a/modules/@angular/router/tsconfig.json b/modules/@angular/router/tsconfig.json index 18bbfd7760..da9d6d583e 100644 --- a/modules/@angular/router/tsconfig.json +++ b/modules/@angular/router/tsconfig.json @@ -8,6 +8,8 @@ "module": "commonjs", "moduleResolution": "node", "noEmitOnError": false, + "noImplicitAny": true, + "noImplicitReturns": true, "outDir": "../../../dist/all/@angular/router/", "paths": { "@angular/*": ["../../../dist/all/@angular/*"] diff --git a/tools/public_api_guard/common/index.d.ts b/tools/public_api_guard/common/index.d.ts index 78a1b58705..cf5db0a346 100644 --- a/tools/public_api_guard/common/index.d.ts +++ b/tools/public_api_guard/common/index.d.ts @@ -197,7 +197,7 @@ export declare class HashLocationStrategy extends LocationStrategy { forward(): void; getBaseHref(): string; onPopState(fn: UrlChangeListener): void; - path(): string; + path(includeHash?: boolean): string; prepareExternalUrl(internal: string): string; pushState(state: any, title: string, path: string, queryParams: string): void; replaceState(state: any, title: string, path: string, queryParams: string): void; @@ -231,7 +231,7 @@ export declare class Location { go(path: string, query?: string): void; isCurrentPathEqualTo(path: string, query?: string): boolean; normalize(url: string): string; - path(): string; + path(includeHash?: boolean): string; prepareExternalUrl(url: string): string; replaceState(path: string, query?: string): void; subscribe(onNext: (value: any) => void, onThrow?: (exception: any) => void, onReturn?: () => void): Object; @@ -246,7 +246,7 @@ export declare abstract class LocationStrategy { abstract forward(): void; abstract getBaseHref(): string; abstract onPopState(fn: UrlChangeListener): void; - 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; @@ -495,7 +495,7 @@ export declare class PathLocationStrategy extends LocationStrategy { forward(): void; getBaseHref(): string; onPopState(fn: UrlChangeListener): void; - path(): string; + path(includeHash?: boolean): string; prepareExternalUrl(internal: string): string; pushState(state: any, title: string, url: string, queryParams: string): void; replaceState(state: any, title: string, url: string, queryParams: string): void;