Shubham Dubey
Shubham Dubey

Reputation: 123

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.

Upvotes: 7

Views: 4667

Answers (2)

StepUp
StepUp

Reputation: 38189

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');

  });
}

Upvotes: 5

Aviso
Aviso

Reputation: 695

You can't have duplicate name for parameters in the route it will overwrite the values.

Use some other name like this.

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

Upvotes: 0

Related Questions