Angular routerLinkActive not working as expected












1














I am working on a rather simple project but this issue has me stumped!



I have routing setup and working correctly except for one small issue with the routerLinkActive not staying activated.



I can't embed an image in a question yet, so please follow the link to take a look at the results I am getting



Routing Results



When I go to: http://localhost:4200/1 the '1' menu item correctly shows as active in the nav bar. If I then go to a child route: http://localhost:4200/1/top100 the '1' menu still stays set as active. This is exactly how I want it to work.



However in a very similar setup I have the '4' menu item go to: http://localhost:4200/2/A. This correctly shows the '4' in the top nav bar as active and the 'Test A' button as active. But when I go to http://localhost:4200/2/B then the 'Test B' button correctly shows as active but the main nav bar as lost its active flag on the '4'.



Code snippets attached below:



Nav Bar



<ul class="navbar-nav">
<li class="nav-item" routerLinkActive="active">
<a class="nav-link" routerLink="/1">1</a>
</li>
<li class="nav-item" routerLinkActive="active">
<a class="nav-link" [routerLink]="['/notUsed', 'notUsed']">2</a>
</li>
<li class="nav-item" routerLinkActive="active">
<a class="nav-link" [routerLink]="['/notUsed', 'notUsed']">3</a>
</li>
<li class="nav-item" routerLinkActive="active">
<a class="nav-link" [routerLink]="['/2', 'A']">4</a>
</li>
<li class="nav-item" routerLinkActive="active">
<a class="nav-link" routerLink="/notUsed">5</a>
</li>
</ul>


Button Group



<div class="btn-group-vertical fixedWidthButtonHonour mb-3" role="group" aria-label="Senior Grades">
<button type="button" class="btn btn btn-outline-danger" [routerLink]="['/2', 'A']" routerLinkActive="active">Test A</button>
<button type="button" class="btn btn btn-outline-danger" [routerLink]="['/2', 'B']" routerLinkActive="active">Test B</button>
</div>


Routes



const appRoutes: Routes = [
{ path: '', component: HomeComponent, pathMatch: 'full' },
{ path: '1', component: Component1 },
{ path: '1/top100/:name', component: ComponentTop100 },
{ path: '2', component: Component2 },
{ path: '2/:viewType', component: Component2 },
{ path: '**', redirectTo: '/', pathMatch: 'full' }
];


I can't work out the difference between the two examples. Great if anyone has any insights or working demo's that achieve what I am after.



Thanks
David










share|improve this question



























    1














    I am working on a rather simple project but this issue has me stumped!



    I have routing setup and working correctly except for one small issue with the routerLinkActive not staying activated.



    I can't embed an image in a question yet, so please follow the link to take a look at the results I am getting



    Routing Results



    When I go to: http://localhost:4200/1 the '1' menu item correctly shows as active in the nav bar. If I then go to a child route: http://localhost:4200/1/top100 the '1' menu still stays set as active. This is exactly how I want it to work.



    However in a very similar setup I have the '4' menu item go to: http://localhost:4200/2/A. This correctly shows the '4' in the top nav bar as active and the 'Test A' button as active. But when I go to http://localhost:4200/2/B then the 'Test B' button correctly shows as active but the main nav bar as lost its active flag on the '4'.



    Code snippets attached below:



    Nav Bar



    <ul class="navbar-nav">
    <li class="nav-item" routerLinkActive="active">
    <a class="nav-link" routerLink="/1">1</a>
    </li>
    <li class="nav-item" routerLinkActive="active">
    <a class="nav-link" [routerLink]="['/notUsed', 'notUsed']">2</a>
    </li>
    <li class="nav-item" routerLinkActive="active">
    <a class="nav-link" [routerLink]="['/notUsed', 'notUsed']">3</a>
    </li>
    <li class="nav-item" routerLinkActive="active">
    <a class="nav-link" [routerLink]="['/2', 'A']">4</a>
    </li>
    <li class="nav-item" routerLinkActive="active">
    <a class="nav-link" routerLink="/notUsed">5</a>
    </li>
    </ul>


    Button Group



    <div class="btn-group-vertical fixedWidthButtonHonour mb-3" role="group" aria-label="Senior Grades">
    <button type="button" class="btn btn btn-outline-danger" [routerLink]="['/2', 'A']" routerLinkActive="active">Test A</button>
    <button type="button" class="btn btn btn-outline-danger" [routerLink]="['/2', 'B']" routerLinkActive="active">Test B</button>
    </div>


    Routes



    const appRoutes: Routes = [
    { path: '', component: HomeComponent, pathMatch: 'full' },
    { path: '1', component: Component1 },
    { path: '1/top100/:name', component: ComponentTop100 },
    { path: '2', component: Component2 },
    { path: '2/:viewType', component: Component2 },
    { path: '**', redirectTo: '/', pathMatch: 'full' }
    ];


    I can't work out the difference between the two examples. Great if anyone has any insights or working demo's that achieve what I am after.



    Thanks
    David










    share|improve this question

























      1












      1








      1







      I am working on a rather simple project but this issue has me stumped!



      I have routing setup and working correctly except for one small issue with the routerLinkActive not staying activated.



      I can't embed an image in a question yet, so please follow the link to take a look at the results I am getting



      Routing Results



      When I go to: http://localhost:4200/1 the '1' menu item correctly shows as active in the nav bar. If I then go to a child route: http://localhost:4200/1/top100 the '1' menu still stays set as active. This is exactly how I want it to work.



      However in a very similar setup I have the '4' menu item go to: http://localhost:4200/2/A. This correctly shows the '4' in the top nav bar as active and the 'Test A' button as active. But when I go to http://localhost:4200/2/B then the 'Test B' button correctly shows as active but the main nav bar as lost its active flag on the '4'.



      Code snippets attached below:



      Nav Bar



      <ul class="navbar-nav">
      <li class="nav-item" routerLinkActive="active">
      <a class="nav-link" routerLink="/1">1</a>
      </li>
      <li class="nav-item" routerLinkActive="active">
      <a class="nav-link" [routerLink]="['/notUsed', 'notUsed']">2</a>
      </li>
      <li class="nav-item" routerLinkActive="active">
      <a class="nav-link" [routerLink]="['/notUsed', 'notUsed']">3</a>
      </li>
      <li class="nav-item" routerLinkActive="active">
      <a class="nav-link" [routerLink]="['/2', 'A']">4</a>
      </li>
      <li class="nav-item" routerLinkActive="active">
      <a class="nav-link" routerLink="/notUsed">5</a>
      </li>
      </ul>


      Button Group



      <div class="btn-group-vertical fixedWidthButtonHonour mb-3" role="group" aria-label="Senior Grades">
      <button type="button" class="btn btn btn-outline-danger" [routerLink]="['/2', 'A']" routerLinkActive="active">Test A</button>
      <button type="button" class="btn btn btn-outline-danger" [routerLink]="['/2', 'B']" routerLinkActive="active">Test B</button>
      </div>


      Routes



      const appRoutes: Routes = [
      { path: '', component: HomeComponent, pathMatch: 'full' },
      { path: '1', component: Component1 },
      { path: '1/top100/:name', component: ComponentTop100 },
      { path: '2', component: Component2 },
      { path: '2/:viewType', component: Component2 },
      { path: '**', redirectTo: '/', pathMatch: 'full' }
      ];


      I can't work out the difference between the two examples. Great if anyone has any insights or working demo's that achieve what I am after.



      Thanks
      David










      share|improve this question













      I am working on a rather simple project but this issue has me stumped!



      I have routing setup and working correctly except for one small issue with the routerLinkActive not staying activated.



      I can't embed an image in a question yet, so please follow the link to take a look at the results I am getting



      Routing Results



      When I go to: http://localhost:4200/1 the '1' menu item correctly shows as active in the nav bar. If I then go to a child route: http://localhost:4200/1/top100 the '1' menu still stays set as active. This is exactly how I want it to work.



      However in a very similar setup I have the '4' menu item go to: http://localhost:4200/2/A. This correctly shows the '4' in the top nav bar as active and the 'Test A' button as active. But when I go to http://localhost:4200/2/B then the 'Test B' button correctly shows as active but the main nav bar as lost its active flag on the '4'.



      Code snippets attached below:



      Nav Bar



      <ul class="navbar-nav">
      <li class="nav-item" routerLinkActive="active">
      <a class="nav-link" routerLink="/1">1</a>
      </li>
      <li class="nav-item" routerLinkActive="active">
      <a class="nav-link" [routerLink]="['/notUsed', 'notUsed']">2</a>
      </li>
      <li class="nav-item" routerLinkActive="active">
      <a class="nav-link" [routerLink]="['/notUsed', 'notUsed']">3</a>
      </li>
      <li class="nav-item" routerLinkActive="active">
      <a class="nav-link" [routerLink]="['/2', 'A']">4</a>
      </li>
      <li class="nav-item" routerLinkActive="active">
      <a class="nav-link" routerLink="/notUsed">5</a>
      </li>
      </ul>


      Button Group



      <div class="btn-group-vertical fixedWidthButtonHonour mb-3" role="group" aria-label="Senior Grades">
      <button type="button" class="btn btn btn-outline-danger" [routerLink]="['/2', 'A']" routerLinkActive="active">Test A</button>
      <button type="button" class="btn btn btn-outline-danger" [routerLink]="['/2', 'B']" routerLinkActive="active">Test B</button>
      </div>


      Routes



      const appRoutes: Routes = [
      { path: '', component: HomeComponent, pathMatch: 'full' },
      { path: '1', component: Component1 },
      { path: '1/top100/:name', component: ComponentTop100 },
      { path: '2', component: Component2 },
      { path: '2/:viewType', component: Component2 },
      { path: '**', redirectTo: '/', pathMatch: 'full' }
      ];


      I can't work out the difference between the two examples. Great if anyone has any insights or working demo's that achieve what I am after.



      Thanks
      David







      angular angular-routing routerlinkactive






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 12 at 11:34









      dkearsley

      123




      123
























          1 Answer
          1






          active

          oldest

          votes


















          0














          The problem is that you expect routerLinkActive on ['/2', 'A'] to get active when you are at ['/2', 'B']. This is not how it works. You should create a routerLinkActive on a /2 link.



          If you don't mind to always redirect /2 to /2/A, you can do the following:



          In your Nav Bar:



          <li class="nav-item" routerLinkActive="active">
          <a class="nav-link" routerLink="/2">4</a>
          </li>


          In your App Routes



          const appRoutes: Routes = [
          { path: '', component: HomeComponent, pathMatch: 'full' },
          { path: '1', component: Component1 },
          { path: '1/top100/:name', component: ComponentTop100 },
          { path: '2', redirectTo: '2/A', pathMatch: 'full' },
          { path: '2/:viewType', component: Component2 },
          { path: '**', redirectTo: '/', pathMatch: 'full' }
          ];


          Another solution could be to just manually add the [class.active] based on your current router path






          share|improve this answer



















          • 1




            Thank you! Excellent answer and solved my problem. I'd not thought about using redirection - good easy suggestion.
            – dkearsley
            Nov 12 at 11:48











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53261313%2fangular-routerlinkactive-not-working-as-expected%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          The problem is that you expect routerLinkActive on ['/2', 'A'] to get active when you are at ['/2', 'B']. This is not how it works. You should create a routerLinkActive on a /2 link.



          If you don't mind to always redirect /2 to /2/A, you can do the following:



          In your Nav Bar:



          <li class="nav-item" routerLinkActive="active">
          <a class="nav-link" routerLink="/2">4</a>
          </li>


          In your App Routes



          const appRoutes: Routes = [
          { path: '', component: HomeComponent, pathMatch: 'full' },
          { path: '1', component: Component1 },
          { path: '1/top100/:name', component: ComponentTop100 },
          { path: '2', redirectTo: '2/A', pathMatch: 'full' },
          { path: '2/:viewType', component: Component2 },
          { path: '**', redirectTo: '/', pathMatch: 'full' }
          ];


          Another solution could be to just manually add the [class.active] based on your current router path






          share|improve this answer



















          • 1




            Thank you! Excellent answer and solved my problem. I'd not thought about using redirection - good easy suggestion.
            – dkearsley
            Nov 12 at 11:48
















          0














          The problem is that you expect routerLinkActive on ['/2', 'A'] to get active when you are at ['/2', 'B']. This is not how it works. You should create a routerLinkActive on a /2 link.



          If you don't mind to always redirect /2 to /2/A, you can do the following:



          In your Nav Bar:



          <li class="nav-item" routerLinkActive="active">
          <a class="nav-link" routerLink="/2">4</a>
          </li>


          In your App Routes



          const appRoutes: Routes = [
          { path: '', component: HomeComponent, pathMatch: 'full' },
          { path: '1', component: Component1 },
          { path: '1/top100/:name', component: ComponentTop100 },
          { path: '2', redirectTo: '2/A', pathMatch: 'full' },
          { path: '2/:viewType', component: Component2 },
          { path: '**', redirectTo: '/', pathMatch: 'full' }
          ];


          Another solution could be to just manually add the [class.active] based on your current router path






          share|improve this answer



















          • 1




            Thank you! Excellent answer and solved my problem. I'd not thought about using redirection - good easy suggestion.
            – dkearsley
            Nov 12 at 11:48














          0












          0








          0






          The problem is that you expect routerLinkActive on ['/2', 'A'] to get active when you are at ['/2', 'B']. This is not how it works. You should create a routerLinkActive on a /2 link.



          If you don't mind to always redirect /2 to /2/A, you can do the following:



          In your Nav Bar:



          <li class="nav-item" routerLinkActive="active">
          <a class="nav-link" routerLink="/2">4</a>
          </li>


          In your App Routes



          const appRoutes: Routes = [
          { path: '', component: HomeComponent, pathMatch: 'full' },
          { path: '1', component: Component1 },
          { path: '1/top100/:name', component: ComponentTop100 },
          { path: '2', redirectTo: '2/A', pathMatch: 'full' },
          { path: '2/:viewType', component: Component2 },
          { path: '**', redirectTo: '/', pathMatch: 'full' }
          ];


          Another solution could be to just manually add the [class.active] based on your current router path






          share|improve this answer














          The problem is that you expect routerLinkActive on ['/2', 'A'] to get active when you are at ['/2', 'B']. This is not how it works. You should create a routerLinkActive on a /2 link.



          If you don't mind to always redirect /2 to /2/A, you can do the following:



          In your Nav Bar:



          <li class="nav-item" routerLinkActive="active">
          <a class="nav-link" routerLink="/2">4</a>
          </li>


          In your App Routes



          const appRoutes: Routes = [
          { path: '', component: HomeComponent, pathMatch: 'full' },
          { path: '1', component: Component1 },
          { path: '1/top100/:name', component: ComponentTop100 },
          { path: '2', redirectTo: '2/A', pathMatch: 'full' },
          { path: '2/:viewType', component: Component2 },
          { path: '**', redirectTo: '/', pathMatch: 'full' }
          ];


          Another solution could be to just manually add the [class.active] based on your current router path







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 12 at 11:46

























          answered Nov 12 at 11:40









          PierreDuc

          28.5k45376




          28.5k45376








          • 1




            Thank you! Excellent answer and solved my problem. I'd not thought about using redirection - good easy suggestion.
            – dkearsley
            Nov 12 at 11:48














          • 1




            Thank you! Excellent answer and solved my problem. I'd not thought about using redirection - good easy suggestion.
            – dkearsley
            Nov 12 at 11:48








          1




          1




          Thank you! Excellent answer and solved my problem. I'd not thought about using redirection - good easy suggestion.
          – dkearsley
          Nov 12 at 11:48




          Thank you! Excellent answer and solved my problem. I'd not thought about using redirection - good easy suggestion.
          – dkearsley
          Nov 12 at 11:48


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53261313%2fangular-routerlinkactive-not-working-as-expected%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          List item for chat from Array inside array React Native

          Thiostrepton

          Caerphilly