Pascal
Pascal

Reputation: 12885

How to router.navigate to a route with an id placeholder inmidst the route url

I have a route url like:

    /departments/:id/employees/assign'

When I navigate to the above url:

    this._router.navigate(['/departments/:id/employees/assign', id]);

Then angular is always appending the passed id to the end of the url, but that is not what I want.

When I try this

    this._router.navigate(['/departments/:id/employees/assign', {id: id}]);

I get this url in the browser:

    http://localhost:4200/departments/%3Aid/employees/assign;id=1

I want the url to be in the browser: /departments/1/employees/assign

What do I have to change?

Upvotes: 6

Views: 18974

Answers (2)

Dave V
Dave V

Reputation: 1976

Use template literals to form dynamic urls like this:

[`/departments/${id}/employees/assign`]

Upvotes: 6

laurent bourdeau
laurent bourdeau

Reputation: 61

According to doc https://angular.io/docs/ts/latest/api/router/index/Router-class.html, you should choose either to user navigateByUrl :

router.navigateByUrl("/departments/:id/employees/assign");

or split the components of the url in parts :

router.navigate(['departments', 1, 'employees', assign], {relativeTo: route});

Upvotes: 6

Related Questions