How to set Bootstrap navbar “active” class in Angular 2?

后端 未结 11 586
被撕碎了的回忆
被撕碎了的回忆 2020-12-07 18:36

How can I set Bootstrap navbar \"active\" class in Angular 2? I only found Angular 1 way.

When I go to About page, add class=\"active\"

相关标签:
11条回答
  • 2020-12-07 18:46
    import { Directive, HostListener, ElementRef, HostBinding, Renderer2 } from '@angular/core';
    
    @Directive({
      selector: '[appNavDropdown]'
    })
    export class NavDropdownDirective {
      isOpen:boolean =false;
      counter:number = 0;
      constructor(private el: ElementRef, private render: Renderer2) { }
    
      @HostBinding('class.open') get opened() {
        return this.isOpen;
      }
    
      @HostListener('click') onmouseClick()
      {
        this.counter++
        console.log();
        if(this.counter % 2 == 1)//odd
        {
          let part = this.render.parentNode(this.el.nativeElement);
          this.render.addClass(part,'open');
          this.isOpen = true;
        }else{
          let part = this.render.parentNode(this.el.nativeElement);
          this.render.removeClass(part,'open');
          this.isOpen = false;
        }
      }
      // @HostListener('mouseleave') onmouseLeave()
      // {
      //   let part = this.render.parentNode(this.el.nativeElement);
      //   this.render.removeClass(part,'open');
      //   this.isOpen = false;
      // }
    
      toggleClose() {
        // let part = this.render.parentNode(this.el.nativeElement)
        //this.menuclick = false;
    
      }
      toggle() {
        this.el.nativeElement.classList.toggle('open');
      }
    }
    
    /**
    * Allows the dropdown to be toggled via click.
    */
    @Directive({
      selector: '[appNavDropdownToggle]'
    })
    export class NavDropdownToggleDirective {
      constructor(private dropdown: NavDropdownDirective) { }
    
      @HostListener('click', ['$event'])
      toggleOpen($event: any) {
        console.log($event)
        $event.preventDefault();
      //  this.dropdown.toggleClose()
       this.dropdown.toggle();
      }
    }
    
    export const NAV_DROPDOWN_DIRECTIVES = [NavDropdownDirective, NavDropdownToggleDirective];
    
    0 讨论(0)
  • 2020-12-07 18:49

    Bert Deterd's answer is correct, but there's one thing that can be added.

    If one route is a substring of another route, you will see something like this happen: 2 active anchors

    You can add this to make it match exact routes only:

    [routerLinkActiveOptions]="{exact:true}"
    

    Full Example:

    <ul class="nav navbar-nav">
      <li [routerLinkActive]="['active']" [routerLinkActiveOptions]="{exact:true}">
        <a [routerLink]="['/']">Home</a>
      </li>
      <li [routerLinkActive]="['active']" [routerLinkActiveOptions]="{exact:true}">
        <a [routerLink]="['/about']">About</a>
      </li>
      <li [routerLinkActive]="['active']" [routerLinkActiveOptions]="{exact:true}">
        <a [routerLink]="['/calendar']">Calendar</a>
      </li>
    </ul>
    
    0 讨论(0)
  • 2020-12-07 18:52

    This does the trick (using RC5)

    <li [class.active]="homeLink.classList.contains('active')">
        <a #homeLink routerLink="/home" routerLinkActive="active">Home</a>
    </li>
    
    0 讨论(0)
  • 2020-12-07 18:55

    In the case of multiple links try this

    <ul class="navbar-nav ml-auto"> 
    <li class="nav-item " routerLink="" routerLinkActive="active" [routerLinkActiveOptions]="{exact:true}">
    <a class="nav-link" routerLink="">Home </a></li>
    <li class="nav-item" [routerLinkActive]="['active']">
    <a class="nav-link" routerLink="/aboutus">About</a></li>
    <li class="nav-item" [routerLinkActive]="['active']">
    <a class="nav-link" routerLink="/gallery">Gallery</a>
     </li>
    <li class="nav-item" [routerLinkActive]="['active']">
    <a class="nav-link" routerLink="/contactus">Contact Us</a>
    </li>
    </ul>
    
    0 讨论(0)
  • 2020-12-07 18:56

    Use the isRouteActive with generate from the Router class.

    According to docs:

    generate(linkParams: any[]) : Instruction

    Generate an Instruction based on the provided Route Link DSL.

    and

    isRouteActive(instruction: Instruction) : boolean

    Given an instruction, returns true if the instruction is currently active, otherwise false.

    <li [class.active]="router.isRouteActive(router.generate(['/Home']))">
       <a [routerLink]="['/Home']">Home</a>
    </li>
    
    0 讨论(0)
  • 2020-12-07 18:57

    On RC2 that did not work for me. I ended up using

       <li  (click)="setActive('home')" class="{{getActive('home')}}"> <a [routerLink]="['/FullScreen']">Home</a></li>
    

    and in the code behind tracked it

        currentChoice: string = "home";
    
       setActive(choice: string): void{
           this.currentChoice = choice;
       }
    
       getActive(choice: string) : string{
           if(this.currentChoice == choice)
                return "active";
           else
                return "not";
    
       }
    
    0 讨论(0)
提交回复
热议问题