0

I am building an app with angular4 and bootstrap 3.3.7 classes. I have a list:

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

When I click in any o the items the class is assign correctly and I can see which one was selected(seeing that it is highlighted), but when I use the browser back button, the isActive function is called and I see it it works as expected(returns true when the URL matches the route being evaluated) but the class is not applied, so I can not see what item is selected even if I am seeing the right router-outlet.

I have algo tried using this approach:

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

but the result is the same, the method is called and retunrs the expected value but the item is not highlighted.

This is my 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"
  }
}

What am I missing? let me know if any other info would be helpful.

Gurwinder Singh
  • 38,557
  • 6
  • 51
  • 76

1 Answers1

0

you can use routerLinkActive directive:

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

Also, in bootstrap, you want to apply active class to li instead:

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

Note that for a simple link like the one you are using, you can directly apply the string value instead of array notation.

Gurwinder Singh
  • 38,557
  • 6
  • 51
  • 76
  • That was my first approach, but it does not work when u press the back button of the browser, I tried this because it was suggested in this question: [link](https://stackoverflow.com/questions/39271654/routerlinkactive-for-routerlink-with-parameters-dynamic/40528170#40528170) – Tomas Maldonado Sep 16 '17 at 17:13
  • @Tomas - Can you share a plunker reproducing your problem? – Gurwinder Singh Sep 16 '17 at 17:56
  • @Tomas For your static links, this approach should work just fine. Please share a Plunker demo to reproduce the issue if any. – Gurwinder Singh Sep 16 '17 at 18:27
  • Plunker reproducing the issue: [plunker](https://plnkr.co/edit/XBfPTS1zBpwRYlI6cG9h?p=preview) – Tomas Maldonado Sep 17 '17 at 04:21