0

I am new to Angular.

I wanted to navigate different component based on the drop down value using Angular 6.

I have used router Module and am getting the values from drop down, but how to navigate to component based on the value of the drop down.

find the code

1> app.routing.module

const routes: Routes = [
  {path:'about',component : AboutComponent},
  {path:'home',component : HomeComponent}
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }

2> app.component.ts

export class AppComponent {

  navLinks = [
    { path : 'home', label : 'Home', id: 1 },
    { path : 'about', label : 'About', id: 2 }
  ];

3> app.component.html

<nav>
    <select id="department" name="department" [(ngModel)]="department" class="form-control">
        <option *ngFor="let links of navLinks" [value]="links.id" [routerLink]="links.path" routerLinkActive #rla="routerLinkActive"> 
          {{links.label}}
        </option>
      </select>
</nav>
mohan
  • 447
  • 4
  • 11
  • 26
  • Seems the code you've provided works fine. https://stackblitz.com/edit/angular-yxgjve What exactly is not working? – Jason White May 16 '19 at 11:15
  • Hi Jason, thanks for your comment, i am not able to load those component and the url also not changing. – mohan May 16 '19 at 11:23
  • Possible duplicate of [Angular 2 RouterLink for Select](https://stackoverflow.com/questions/37368491/angular-2-routerlink-for-select) – noamyg May 16 '19 at 11:34

2 Answers2

1

routerLink and routerLinkActive won't work out of the box with select. Bind the selected value to an element, then navigate on select. Here's the full solution: https://stackblitz.com/edit/angular-ualqhw

app.component.ts:

import { Component } from '@angular/core';
import { Router } from '@angular/router';

export interface INavLink {
  id : number; 
  pathLink : string;
  label : string;
}

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

export class AppComponent  {
  name = 'Angular';

  selectedNavLink : INavLink;
  navLinks : Array<INavLink> = [
    { pathLink : '/home', label : 'Home', id: 1 },
    { pathLink : '/about', label : 'About', id: 2 }
  ];

  constructor(private router : Router){}

  routeToLink = (event : Event) => {
     this.router.navigate([this.selectedNavLink.pathLink]);

  }

}

app.component.html:

<nav>
    <select (change)="routeToLink()" id="department" name="department" [(ngModel)]="selectedNavLink" class="form-control">
        <option *ngFor="let link of navLinks" [ngValue]="link"> 
          {{link.label}}
        </option>
      </select>
</nav>
noamyg
  • 2,747
  • 1
  • 23
  • 44
0

You can achieve this by injecting Router service in your component and by using navigate method

Your HTML would be like this

<nav>
    <select id="department" name="department" [(ngModel)]="department" class="form-control" (change)="navigate($event.target.value)">
        <option *ngFor="let links of navLinks" [value]="links.path" [routerLink]="links.path" routerLinkActive #rla="routerLinkActive"> 
          {{links.label}}
        </option>
      </select>
</nav>

Your component would look like this:

export class AppComponent  {
  navLinks = [
    { path : 'home', label : 'Home', id: 1 },
    { path : 'about', label : 'About', id: 2 }
  ];
  name = 'Angular';

  constructor(private router: Router) {

  }

  navigate(path) {
    this.router.navigate(['/' + path])
  }
}

EDIT

routerLink won't work for your select control since select does not have a click event which is captured by the RouterLink

// ** Code below is copied from Angular source code on GitHub. **
@HostListener("click")
  onClick(): boolean {
    // If no target, or if target is _self, prevent default browser behavior
    if (!isString(this.target) || this.target == '_self') {
      this._router.navigate(this._commands, this._routeSegment);
      return false;
    }
    return true;
  }

More on Routing here

asimhashmi
  • 4,258
  • 1
  • 14
  • 34
  • Thanks Asim, but i have a question. Why should i call this navigate method of navigation, why it is not working with the [routerLink]="links.path" in the html? – mohan May 16 '19 at 11:32
  • Because routerLink (https://angular.io/api/router/RouterLink) is a directive listening to `onclick` events, and ` – noamyg May 16 '19 at 11:41