Overview Of Route Parameters In Angular

Introduction

In this article, we are going to send data from one template to another through route parameters.

Prerequisites

Before we start, please go through the creation of an application using the route in Angular.

Create a new component.

  • >ng g c student
  • >ng g c student-list

Note: we are going to use the same application with two newly-added components. We will try to add a student detail (only name) and pass that to the Added details component through route and routing parameters.

Open student.component.html and add the below code.

  1. <h3>Add Student</h3>  
  2. <label>Enter name:</label>  
  3. <input #name type="text">    
  4. <button (click)="AddStudent(name.value)">Add</button>   

Open student.component.ts and add the below content.

  1. import { Component, OnInit } from '@angular/core';  
  2. import { Router } from '@angular/router';  
  3.   
  4. @Component({  
  5.   selector: 'app-student',  
  6.   templateUrl: './student.component.html',  
  7.   styleUrls: ['./student.component.css']  
  8. })  
  9. export class StudentComponent implements OnInit {  
  10.   AddStudent(obj){  
  11.     this.router.navigate(['/studentDetails', obj]);  
  12.   }  
  13.   constructor(private router : Router) { }  
  14.   ngOnInit() {  
  15.   }  
  16. }  

Open student-details.component.html and add the below lines.

  1. <h2>Added Student Details</h2>  
  2. <p>Name : <b>{{studentName}}</b> --- added successfully.</p>  

Open student-details.component.ts and add the following code.

  1. import { Component, OnInit } from '@angular/core'
  2. import { StudentService } from '../student.service';  
  3. import { ActivatedRoute } from '@angular/router';  
  4.   
  5. @Component({  
  6.   selector: 'app-student-details',  
  7.   templateUrl: './student-details.component.html',  
  8.   styleUrls: ['./student-details.component.css']  
  9. })  
  10. export class StudentDetailsComponent implements OnInit {  
  11.   
  12.   public studentName = "";  
  13.   constructor(private route : ActivatedRoute) {   
  14.   }  
  15.   
  16.   ngOnInit() {  
  17.     let name = this.route.snapshot.paramMap.get('name');  
  18.     this.studentName = name;  
  19.   }  
  20. }  

Open app.component.html and add the below HTML.

  1. <!--The content below is only a placeholder and can be replaced.-->  
  2. <div style="text-align:center">  
  3.   <h1>  
  4.     Welcome to {{ title }}!  
  5.   </h1>  
  6. </div>  
  7. <ul>  
  8.   <li>  
  9.     <a routerLink="/addStudent">Add Student</a>  
  10.   </li>  
  11.   <li>  
  12.     <a routerLink="/studentDetails">Student Details</a>  
  13.   </li>  
  14.   <li>  
  15.     <a routerLink="/studentMarks">Student Marks</a>  
  16.   </li>  
  17. </ul>  
  18. <router-outlet></router-outlet>  

Open app-routing.module.ts and add these lines.

  1. import { NgModule } from '@angular/core';  
  2. import { Routes, RouterModule } from '@angular/router';  
  3. import { StudentDetailsComponent } from './student-details/student-details.component';  
  4. import { StudentMarksComponent } from './student-marks/student-marks.component';  
  5. import { NotFoundComponent } from './not-found/not-found.component';  
  6. import { StudentComponent } from './student/student.component';  
  7. import { StudentListComponent } from './student-list/student-list.component';  
  8.   
  9. const routes:Routes = [  
  10.     {path: '', redirectTo : '/studentDetails', pathMatch : 'full'},  
  11.     {path: 'studentList', component : StudentListComponent},  
  12.     {path: 'studentDetails/:name', component : StudentDetailsComponent},  
  13.     {path: 'studentMarks', component : StudentMarksComponent},  
  14.     {path: 'addStudent', component : StudentComponent},  
  15.     {path: "**", component : NotFoundComponent}  
  16. ];  
  17.   
  18. @NgModule({  
  19.     imports : [RouterModule.forRoot(routes)],  
  20.     exports: [RouterModule]  
  21. })  
  22. export class AppRoutingModule{}  

Run the application.

Click on the "Add Student" link.

Route Parameters In Angular

Enter the name as “Mohammad Irshad” and click the “Add” button.

Route Parameters In Angular 

You can see that the data you send from the "Add Student" View is coming to the "Student Details" View through routing parameter.

You can check the URL. It is having the value we send and that is what we defined in the route list.

What have we done?

  1. Added route with a placeholder for the route parameter.
    1. {path: 'studentDetails/:name', component : StudentDetailsComponent},  
  1. To pass the parameter while routing, we made use of router service. It has navigated the method through which we can pass the link and parameters array.

    See student.component.ts
    1. AddStudent(obj){  
    2.     this.router.navigate(['/studentDetails', obj]);  
    3.   }  
  1. To read the router parameter, we made use of the Activated route. We did use the snapshot of the current route with param API to get and read the parameter. Then, we simply bound that value to the View using interpolation.

See student-details.component.ts

  1. let name = this.route.snapshot.paramMap.get('name');  
  2. this.studentName = name;  

So, this is how we can pass parameter while routing in Angular.

Now, let us pass multiple parameters in routing. Open app-routing.module.ts and add the below contents in routes.

  1. const routes:Routes = [  
  2.     {path: '', redirectTo : '/studentList', pathMatch : 'full'},  
  3.     {path: 'studentList', component : StudentListComponent},  
  4.     {path: 'studentDetails/:id/:name/:marks', component : StudentDetailsComponent},  
  5.     {path: 'studentMarks', component : StudentMarksComponent},  
  6.     {path: 'addStudent', component : StudentComponent},  
  7.     {path: "**", component : NotFoundComponent}  
  8. ];  

Open student.component.html and add the below code.

  1. <h3>Add Student</h3>  
  2. <label>Enter Id:</label>  
  3. <input type="text" [(ngModel)]="student.id">   
  4. <label>Enter name:</label>  
  5. <input type="text" [(ngModel)]="student.name">   
  6. <label>Enter Marks:</label>  
  7. <input type="text" [(ngModel)]="student.marks">    
  8. <button (click)="AddStudent(student)">Add</button>  

Open student.component.ts and add the below code.

  1. import { Component, OnInit } from '@angular/core';  
  2. import { Router } from '@angular/router';  
  3.   
  4. @Component({  
  5.   selector: 'app-student',  
  6.   templateUrl: './student.component.html',  
  7.   styleUrls: ['./student.component.css']  
  8. })  
  9. export class StudentComponent implements OnInit {  
  10.   
  11.   public student : any = {  
  12.     id: "", name : "", marks : ""  
  13.   };  
  14.   AddStudent(obj){  
  15.     this.router.navigate(['/studentDetails', obj.id, obj.name, obj.marks]);  
  16.   }  
  17.   
  18.   constructor(private router : Router) { }  
  19.   
  20.   ngOnInit() {  
  21.   }  
  22.   
  23. }  

Open student-details.component.ts and update the ngOnInit() method.

  1. ngOnInit() {  
  2.   this.student.id = this.route.snapshot.paramMap.get('id');  
  3.   this.student.name = this.route.snapshot.paramMap.get('name');  
  4.   this.student.marks = this.route.snapshot.paramMap.get('marks');  
  5. }  

Open student-details.component.html and add the below code.

  1. <h2>Added Student Details</h2>  
  2. <p>Id : <b>{{student.id}}</b></p>  
  3. <p>Name : <b>{{student.name}}</b></p>  
  4. <p>Marks : <b>{{student.marks}}</b></p>  
  5. <p> --- added successfully.</p>  

Run the application.

Route Parameters In Angular

Enter details and click on the Add button.

Route Parameters In Angular


Similar Articles