How I can change the params id and name with the previous and next buttons? The Id is changing but the name is not changing in the route and component. Also, I wanted to add the limit with ID. I only have the 5 students in the array where I'm getting the student's name and id.
ngOnInit() {
// Display Student ID in the Component
this.route.params.subscribe(data => {
this.stdID = data['id'];
this.stdNAME = data['name'];
console.log(this.stdID);
})
}
previousStudent(){
let previousID = parseInt(this.stdID) - 1;
let previousNAME = this.stdNAME;
this.router.navigate(['/students', previousID, previousNAME])
}
nextStudent(){
let nextID = parseInt(this.stdID) + 1;
let nextNAME = this.stdNAME;
this.router.navigate(['/students', nextID, nextNAME])
}
app.routing.ts
const routes: Routes = [
{ path:'', component: HomeComponent },
{ path:'home', component: HomeComponent, pathMatch: 'full' },
{ path:'student-data', component: StudentDataComponent },
{ path:'students', component: StudentListComponent},
{ path:'students/:id/:name', component: StudentDetailsListComponent},
{ path:'**', component: NotFoundComponent },
];
studentListComponent.html
<p>Student detail works</p>
<h3>This is Student ID : {{stdID}}</h3>
<h3>This is Student NAME : {{stdNAME}}</h3>
<br>
<button type="button" class="btn btn-outline-success" (click)="previousStudent()">Previous Student</button>
<button type="button" class="btn btn-outline-success" (click)="nextStudent()">Next Student</button>
id.StudentDetailListComponent, you should get the injectedStudentServiceand store the previous and next student by current student'sid.Demo @ StackBlitz