2

After a successful login on the login page, the route changes to the "inbound" view, which has two tabs in the navbar, "inbound" and "outbound". I'd like for the "inbound" navtab to be already selected to reflect the state of the router. However, after implementing this demo, it still isn't selected by default

html:

        <nav md-tab-nav-bar *ngIf="!router.url.includes('login')">
            <a md-tab-link 
               *ngFor="let tabLink of tabLinks; let i = index"
               [routerLink]="tabLink.link"
               routerLinkActive #rla="routerLinkActive"
               [active]="rla.isActive">
                {{tabLink.label}}
            </a>
        </nav>

component:

import { Component }            from '@angular/core';
import { Router }               from '@angular/router';
import { MdTab, MdTabLink }     from '@angular/material';

@Component({
    selector: 'header',
    templateUrl: './header.component.html',
    styleUrls: [ './header.component.css' ]
})

export class HeaderComponent {     
    tabLinks = [
        { label: 'Inbound', link: 'inbound' },
        { label: 'Outbound', link: 'outbound' }
    ];
    constructor( private router: Router ) { }
}

routes:

    const appRoutes: Routes = [
    {
        path: '',
        redirectTo: '/inbound',
        pathMatch: 'full'
    },
    {
        path: 'inbound',
        component: InboundMessagesComponent,
        canActivate: [ RouteGuard ]
    },
    {
        path: 'outbound',
        component: OutboundMessagesComponent,
        canActivate: [ RouteGuard ]
    },
    {
        path: 'login',
        component: LoginComponent
    }
];
tyler2cr
  • 73
  • 1
  • 1
  • 9
  • Can you share your routes configuration? – Will Howell Sep 13 '17 at 20:17
  • @WillHowell just added them. I can add the routegaurd as well, but it just checks for a token in the browser. Thanks in advance! – tyler2cr Sep 13 '17 at 20:38
  • And `InboundMessagesComponent` is showing up in the router outlet? Try using absolute paths in your `tabLinks.link` just to be sure – Will Howell Sep 13 '17 at 21:00
  • yeah the component view successfullly renders through the router outlet, and if i then click on the "inbound" or "outbound" tabs the active styling takes affect on the activated tab. By absolute path you mean `http://localhost:3000/inbound`? I tried that and no luck – tyler2cr Sep 13 '17 at 21:12
  • Oh yeah I meant `/inbound`. Other debugging measures might be printing `{{ rla.isActive }}` in your tab label to see if the router link is activating correctly on load. If it is, then perhaps there is a change detection bug in the tab-navs. Otherwise, it'll be some sort of routing issue. – Will Howell Sep 13 '17 at 21:30
  • Also what material version? – Will Howell Sep 13 '17 at 21:30
  • Awesome - `{{ rla.isActive }}` is displaying the correct boolean values so i'll play with some changeDetection. Version is beta-10 – tyler2cr Sep 13 '17 at 22:01
  • I know there have been several routing/tab related issues, but I think they were more to do with the tab body acting strangely. If you can reproduce with a really simplified example, file an issue! Also consider dropping the routerLink stuff and just setting `[active]` with class properties to see if you can nail it down. – Will Howell Sep 13 '17 at 22:11
  • 1
    I think it might have something to do with the `*ngIf`... i'll bet that when it first renders it sees the login url even though it's hidden. i threw in some change detection for kicks to no avail and then it dawned on me - it shouldn't be highlighted anyway since it doesn't stay highlighted after you've clicked elsewhere in the current view. What i should see is the underline, which isn't there at all and i know there is an issue already filed for that. I'll keep an eye on that and will revisit this when its ready. Thanks for your help as always!! – tyler2cr Sep 13 '17 at 23:09

0 Answers0