Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get multiple ids from route param in Angular 6?

I want to fetch multiples ids which I am passing while routing in Angular using route.params. This is the route.ts

{path:'section/:id/:id', component: SubsectionsComponent}

And this is how I am routing from a component.ts

onSectionClick(id1, id2){
    this.router.navigate(['/path/',id1,id2], {relativeTo:this.route})
  }

And this is how I am fetching in the component where it routes.

constructor(private route: ActivateRoute){}
this.route.params.subscribe(
      (route)=>{  
        console.log(route)
      }
    )

But it is only logging one id from the params.

like image 812
Shubham Dubey Avatar asked Sep 24 '19 09:09

Shubham Dubey


Video Answer


1 Answers

You should use different names in your path. For example:

{path:'section/:id1/:id2', component: SubsectionsComponent}

And then it becomes pretty simple:

import { ActivatedRoute } from '@angular/router';

constructor(private route: ActivatedRoute) {
  this.route.paramMap.subscribe( params => {
    this.id_1 = params.get('id1');
    this.id_2 = params.get('id2');

  });
}
like image 51
StepUp Avatar answered Sep 25 '22 16:09

StepUp