Overview Of Optional Route Parameters In Angular

Introduction

In this article, we are going to learn the optional route parameters in Angular. Sometimes it may be a requirement that we want the component view to be rendered on the basis of some values coming from the route parameter or we just want to render the component with some additional functionality with the help of some values coming from parameters.

Prerequisites

Before we start please go through the creation of applications using a route parameter in Angular with paramMap.

Let us try achieving the use of optional route parameters.

We are going to make some small changes to the application. We will have a list of student records, and clicking on any record will display the details of that selected student and on the details page, we will have back button, clicking on which will send you back to the student-list page view with the selected record in some different style.

Open student-list.component.ts and add the below contents,

  1. import { Component, OnInit } from '@angular/core';  
  2. import { ActivatedRoute, Router, ParamMap } from '@angular/router';  
  3.   
  4. @Component({  
  5.   selector: 'app-student-list',  
  6.   templateUrl: './student-list.component.html',  
  7.   styleUrls: ['./student-list.component.css']  
  8. })  
  9. export class StudentListComponent implements OnInit {  
  10.   
  11.   public student = {  
  12.     id: "",   
  13.     name: "",   
  14.     marks: ""  
  15.    };  
  16.   public students = [    
  17.     {"id" : 1001, "name" : "Irshad""marks" : 90},    
  18.     {"id" : 1002, "name" : "Imran""marks" : 80},    
  19.     {"id" : 1003, "name" : "Rahul""marks" : 70},    
  20.     {"id" : 1004, "name" : "Ajay""marks" : 85},    
  21.     {"id" : 1005, "name" : "Sunny""marks" : 60}    
  22.     ];    
  23.   constructor(private route: ActivatedRoute) { }  
  24.   
  25.   ngOnInit() {  
  26.     this.student.id = this.route.snapshot.paramMap.get('id');  
  27.     this.student.name = this.route.snapshot.paramMap.get('name');  
  28.     this.student.marks = this.route.snapshot.paramMap.get('marks');  
  29.   }  
  30. }  

Open student-list.component.html and add the below contents,

  1. <h2>Student Details:</h2>    
  2. <div *ngFor="let stud of students">    
  3.     <a routerLink="/studentDetails/{{stud.id}}/{{stud.name}}/{{stud.marks}}"> Id : {{stud.id}}, Name : {{stud.name}} </a>    
  4. </div>   
  5. <br>  
  6. <div *ngIf="student.id">  
  7.     your current selection : Id - {{student.id}}, Name - {{student.name}}, Marks - {{student.marks}}  
  8. </div>  

Open student-details.component.ts and add the below contents,

  1. import { Component, OnInit } from '@angular/core';  
  2. import { StudentService } from '../student.service';  
  3. import { ActivatedRoute, Router } 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 student = {  
  13.      id: "",   
  14.      name: "",   
  15.      marks: ""  
  16.     };  
  17.   constructor(private route : ActivatedRoute, private router : Router) {   
  18.   }  
  19.   
  20.   ngOnInit() {  
  21.     this.student.id = this.route.snapshot.paramMap.get('id');  
  22.     this.student.name = this.route.snapshot.paramMap.get('name');  
  23.     this.student.marks = this.route.snapshot.paramMap.get('marks');  
  24.   }  
  25.   
  26.   gotoStudentList(){   
  27.     this.router.navigate(['/studentList'this.student])  
  28.   }  
  29. }  

Open student-details.component.html and add the below contents,

  1. <h2>Selected 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.   
  6. <div>  
  7.     <button (click)="gotoStudentList()">Back</button>  
  8. </div>  

Open app-routing.module.ts and change the route values as below,

  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. ];  

Run the application,

Overview Of Optional Route Parameters In Angular 

Url: http://localhost:4200/studentList with no parameters.

Click on any record.

Overview Of Optional Route Parameters In Angular 

Click on the back button,

Overview Of Optional Route Parameters In Angular 

Again we are redirecting the same url http://localhost:4200/studentList but with additional parameters http://localhost:4200/studentList;id=1001;name=Irshad;marks=90. These parameters are optional because its existence does not affect the view but they can be used to apply the logic to the view.

Also you can see the two routes,

  1. {path: 'studentList', component : StudentListComponent},  
  2. {path: 'studentDetails/:id/:name/:marks', component : StudentDetailsComponent},  

The studentList path does not require any parameter to render but still we are accessing it through parameters http://localhost:4200/studentList;id=1001;name=Irshad;marks=90. This is because we are using optional parameters in the form of key value pairs.

  1. this.router.navigate(['/studentList'this.student])  

or

  1. this.router.navigate(['/studentList', {id : this.student.id, name : this.student.name, marks : this.student.marks}])  

So, this is all about optional route parameters that you can use according to the logic applicable on your view.


Similar Articles