已分配ngClass但浏览器未显示样式

时间:2017-09-16 16:42:44

标签: twitter-bootstrap angular twitter-bootstrap-3 angular2-routing

我正在使用angular4和bootstrap 3.3.7类构建应用程序。我有一个清单:

<ul class="nav nav-sidebar">
    <li><a [routerLink]="['/home']" [ngClass]="{'active': isActive(['home'])}">Overview </a></li>
    <li><a [routerLink]="['/businessList']" [ngClass]="{'active': isActive(['businessList'])}">Businesses</a></li>
    <li><a [routerLink]="['/storeList']" [ngClass]="{'active': isActive(['storeList'])}">Stores</a></li>
</ul>

当我点击任何类正确分配的项目时,我可以看到哪一个被选中(看到它被突出显示),但当我使用浏览器后退按钮时,isActive函数被调用,我看到它它按预期工作(当URL与正在评估的路由匹配时返回true)但该类未应用,因此即使我看到正确的路由器插座,我也看不到选择了哪个项目。

我已经尝试使用这种方法:

<li><a [routerLink]="['/storeList']" [class.active]="isActive(['storeList'])">Stores</a></li>

但结果相同,调用该方法并重新调整预期值但该项未突出显示。

这是我的package.json:

{
  "name": "WebApp",
  "version": "0.0.0",
  "license": "MIT",
  "angular-cli": {},
  "scripts": {
    "ng": "ng",
    "start": "ng server --proxy proxy.conf.json",
    "test": "ng test",
    "pree2e": "webdriver-manager update --standalone false --gecko false",
    "e2e": "protractor"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "^4.4.0-RC.0",
    "@angular/common": "^4.4.0-RC.0",
    "@angular/compiler": "^4.4.0-RC.0",
    "@angular/compiler-cli": "^4.4.0-RC.0",
    "@angular/core": "^4.4.0-RC.0",
    "@angular/forms": "^4.4.0-RC.0",
    "@angular/http": "^4.4.0-RC.0",
    "@angular/platform-browser": "^4.4.0-RC.0",
    "@angular/platform-browser-dynamic": "^4.4.0-RC.0",
    "@angular/platform-server": "^4.4.0-RC.0",
    "@angular/router": "^4.4.0-RC.0",
    "bootstrap": "^3.3.7",
    "core-js": "^2.4.1",
    "ng2-file-input": "^0.1.13",
    "ng2-file-upload": "^1.2.1",
    "ngx-bootstrap": "^1.9.3",
    "rxjs": "^5.4.3",
    "ts-helpers": "^1.1.1",
    "typescript": "^2.5.2",
    "zone.js": "^0.8.17"
  },
  "devDependencies": {
    "@angular/compiler-cli": "^4.3.6",
    "@types/jasmine": "2.5.38",
    "@types/node": "^6.0.42",
    "angular-cli": "1.0.0-beta.28.3",
    "codelyzer": "~2.0.0-beta.1",
    "jasmine-core": "2.5.2",
    "jasmine-spec-reporter": "2.5.0",
    "karma": "1.2.0",
    "karma-chrome-launcher": "^2.0.0",
    "karma-cli": "^1.0.1",
    "karma-jasmine": "^1.0.2",
    "karma-remap-istanbul": "^0.2.1",
    "protractor": "~4.0.13",
    "ts-node": "1.2.1",
    "tslint": "^4.3.0",
    "typescript": "~2.0.3"
  }
}

我错过了什么?如果有任何其他信息有用,请告诉我。

1 个答案:

答案 0 :(得分:0)

您可以使用routerLinkActive指令:

<li><a routerLink="/storeList" routerLinkActive="active">Stores</a></li>

此外,在bootstrap中,您希望将active类应用于li

<li routerLinkActive="active"><a routerLink="/storeList">Stores</a></li>

请注意,对于您正在使用的简单链接,您可以直接应用字符串值而不是数组表示法。