7

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.

1
  • use different names in your path. :id1/:id2 for example Commented Sep 24, 2019 at 9:37

2 Answers 2

5

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

  });
}
Sign up to request clarification or add additional context in comments.

Comments

0

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}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.