/** * @license * Copyright Google Inc. All Rights Reserved. * * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file at https://angular.io/license */ import {AfterContentInit, ChangeDetectorRef, ContentChildren, Directive, ElementRef, Input, OnChanges, OnDestroy, QueryList, Renderer, SimpleChanges} from '@angular/core'; import {Subscription} from 'rxjs/Subscription'; import {NavigationEnd} from '../events'; import {Router} from '../router'; import {RouterLink, RouterLinkWithHref} from './router_link'; /** * @whatItDoes Lets you add a CSS class to an element when the link's route becomes active. * * @howToUse * * ``` * Bob * ``` * * @description * * The RouterLinkActive directive lets you add a CSS class to an element when the link's route * becomes active. * * Consider the following example: * * ``` * Bob * ``` * * When the url is either '/user' or '/user/bob', the active-link class will * be added to the `a` tag. If the url changes, the class will be removed. * * You can set more than one class, as follows: * * ``` * Bob * Bob * ``` * * You can configure RouterLinkActive by passing `exact: true`. This will add the classes * only when the url matches the link exactly. * * ``` * Bob * ``` * * You can assign the RouterLinkActive instance to a template variable and directly check * the `isActive` status. * ``` * * Bob {{ rla.isActive ? '(already open)' : ''}} * * ``` * * Finally, you can apply the RouterLinkActive directive to an ancestor of a RouterLink. * * ``` *
* ``` * * This will set the active-link class on the div tag if the url is either '/user/jim' or * '/user/bob'. * * @ngModule RouterModule * * @stable */ @Directive({ selector: '[routerLinkActive]', exportAs: 'routerLinkActive', }) export class RouterLinkActive implements OnChanges, OnDestroy, AfterContentInit { @ContentChildren(RouterLink, {descendants: true}) links: QueryList