如何设置Bootstrap navbar" active" Angular 2中的课程?

时间:2016-02-16 02:18:09

标签: twitter-bootstrap angular nav angular2-routing

如何设置Bootstrap navbar" active" Angular 2中的课程?我只找到Angular 1 way

当我转到关于页面时,将class="active"添加到关于,然后移除主页上的class="active"

<ul class="nav navbar-nav">
    <li class="active"><a [routerLink]="['Home']">Home</a></li>
    <li><a [routerLink]="['About']">About</a></li></li>
</ul>

由于

11 个答案:

答案 0 :(得分:139)

如果您使用新的3.0.0。组件路由器(https://github.com/angular/vladivostok)您可以使用 routerLinkActive 指令。无需进一步的javascript。

<ul class="nav navbar-nav">
  <li [routerLinkActive]="['active']"> <a [routerLink]="['one']">One</a></li>
  <li [routerLinkActive]="['active']"> <a [routerLink]="['second']">Second</a></li>
</ul>

我使用“@ angular / router”:“^ 3.0.0-alpha.7”

答案 1 :(得分:47)

Bert Deterd的回答是正确的,但有一件事可以补充。

如果一条路线是另一条路线的子串,您会看到类似的情况发生:2 active anchors

您可以添加此选项以使其仅与精确路线匹配:

[routerLinkActiveOptions]="{exact:true}"

完整示例:

<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>

答案 2 :(得分:8)

对于最新版本的Angular2 RC4,以下简单代码有效:

      <li [class.active]="router.url==='/about'"><a [routerLink]="['/about']">About</a></li>

从“@ angular / router”使用 import {Router}; 并将路由器放在构造函数中。

答案 3 :(得分:6)

isRouteActive Router 类中的generate一起使用。

根据文件:

  

生成(linkParams:any []):说明

     

根据提供的Route Link DSL生成指令。

  

isRouteActive(指令:指令):布尔

     

给定一条指令,如果当前指令则返回true   积极的,否则是假的。

<li [class.active]="router.isRouteActive(router.generate(['/Home']))">
   <a [routerLink]="['/Home']">Home</a>
</li>

答案 4 :(得分:5)

这就是诀窍(使用RC5)

export interface Person {
    name: string;
    surname: string;
    age: number;
}

let personOk = { name: "John", surname: "Doe", age: 36 };
let personNotOk = { name: 22, age: "x" };

// YES. Now you CAN use an interface as a type reference object.
console.log("isValid(personOk):  " + isValid(personOk, Person) + "\n");
console.log("isValid(personNotOk):  " + isValid(personNotOk, Person) + "\n");

答案 5 :(得分:2)

在RC2上对我不起作用。我最终使用

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

并在跟踪它的代码中

    currentChoice: string = "home";

   setActive(choice: string): void{
       this.currentChoice = choice;
   }

   getActive(choice: string) : string{
       if(this.currentChoice == choice)
            return "active";
       else
            return "not";

   }

答案 6 :(得分:2)

在&#34; @ angular / router&#34;:&#34; ^ 3.3.1&#34;,以前版本的差异是routerLinkActive中的括号

[routerLinkActive]="['active']"

在最终版本中,ng2会抱怨所以只需删除括号

routerLinkActive="active"

答案 7 :(得分:0)

版本&#34; @ angular / router&#34;:&#34; ^ 3.3.1&#34;

我只是把路线的名称,我在app.route.ts

中声明
import { ModuleWithProviders } from '@angular/core';
import { Routes, RouterModule }   from '@angular/router';

import { HomeComponent } from './home/home.component';
import { DeclarationsComponent } from './declarations/declarations.component';

const appRoutes: Routes = [
    { path: '', pathMatch: 'full', component: HomeComponent },
    { path: 'declarations', pathMatch: 'full', component: DeclarationsComponent }
];

export const routing: ModuleWithProviders = RouterModule.forRoot(appRoutes);

在视图中,我只写了路线的名称

  <ul class="nav navbar-nav">
     <li routerLinkActive="active"><a routerLink="">Home</a></li>
     <li><a routerLink="declarations">SAV</a></li>
  </ul>

答案 8 :(得分:0)

<a routerLink="/user/bob" routerLinkActive="active-link" [routerLinkActiveOptions]="{exact:
true}">Bob</a>

答案 9 :(得分:0)

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];

答案 10 :(得分:0)

如果有多个链接 试试这个

<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>