diff --git a/modules/@angular/router/src/directives/router_link_active.ts b/modules/@angular/router/src/directives/router_link_active.ts index b08cf23af3..6459f1bdf7 100644 --- a/modules/@angular/router/src/directives/router_link_active.ts +++ b/modules/@angular/router/src/directives/router_link_active.ts @@ -59,8 +59,9 @@ import {RouterLink, RouterLinkWithHref} from './router_link'; */ @Directive({selector: '[routerLinkActive]'}) export class RouterLinkActive implements OnChanges, OnDestroy, AfterContentInit { - @ContentChildren(RouterLink) links: QueryList; - @ContentChildren(RouterLinkWithHref) linksWithHrefs: QueryList; + @ContentChildren(RouterLink, {descendants: true}) links: QueryList; + @ContentChildren(RouterLinkWithHref, {descendants: true}) + linksWithHrefs: QueryList; private classes: string[] = []; private subscription: Subscription; diff --git a/modules/@angular/router/test/router.spec.ts b/modules/@angular/router/test/router.spec.ts index 93de765411..d6b632ee81 100644 --- a/modules/@angular/router/test/router.spec.ts +++ b/modules/@angular/router/test/router.spec.ts @@ -1226,34 +1226,35 @@ describe('Integration', () => { it('should set the class on a parent element when the link is active', - fakeAsync(inject( - [Router, TestComponentBuilder, Location], - (router: Router, tcb: TestComponentBuilder, location: Location) => { - const fixture = createRoot(tcb, router, RootCmp); + fakeAsync(inject( + [Router, TestComponentBuilder, Location], + (router: Router, tcb: TestComponentBuilder, location: Location) => { + const fixture = createRoot(tcb, router, RootCmp); - router.resetConfig([{ - path: 'team/:id', - component: TeamCmp, - children: [{ - path: 'link', - component: DummyLinkWithParentCmp, - children: - [{path: 'simple', component: SimpleCmp}, {path: '', component: BlankCmp}] - }] - }]); + router.resetConfig([{ + path: 'team/:id', + component: TeamCmp, + children: [{ + path: 'link', + component: DummyLinkWithParentCmp, + children: [ + {path: 'simple', component: SimpleCmp}, {path: '', component: BlankCmp} + ] + }] + }]); - router.navigateByUrl('/team/22/link;exact=true'); - advance(fixture); - expect(location.path()).toEqual('/team/22/link;exact=true'); + router.navigateByUrl('/team/22/link;exact=true'); + advance(fixture); + expect(location.path()).toEqual('/team/22/link;exact=true'); - const native = fixture.debugElement.nativeElement.querySelector('link-parent'); - expect(native.className).toEqual('active'); + const native = fixture.debugElement.nativeElement.querySelector('link-parent'); + expect(native.className).toEqual('active'); - router.navigateByUrl('/team/22/link/simple'); - advance(fixture); - expect(location.path()).toEqual('/team/22/link/simple'); - expect(native.className).toEqual(''); - }))); + router.navigateByUrl('/team/22/link/simple'); + advance(fixture); + expect(location.path()).toEqual('/team/22/link/simple'); + expect(native.className).toEqual(''); + }))); it('should set the class when the link is active', fakeAsync(inject( @@ -1439,17 +1440,6 @@ class DummyLinkCmp { constructor(route: ActivatedRoute) { this.exact = (route.snapshot.params).exact === 'true'; } } -@Component({ - selector: 'link-cmp', - template: - `link`, - directives: ROUTER_DIRECTIVES -}) -class DummyLinkWithParentCmp { - private exact: boolean; - constructor(route: ActivatedRoute) { this.exact = (route.snapshot.params).exact === 'true'; } -} - @Component({ selector: 'link-cmp', template: `link`, @@ -1566,6 +1556,19 @@ class LinkInNgIf { alwaysTrue = true; } +@Component({ + selector: 'link-cmp', + template: ` + + + `, + directives: ROUTER_DIRECTIVES +}) +class DummyLinkWithParentCmp { + private exact: boolean; + constructor(route: ActivatedRoute) { this.exact = (route.snapshot.params).exact === 'true'; } +} + @Component({ selector: 'root-cmp', template: ``,