6

I have dynamic routes that could contain slashes or anti-slashes inside parameters , for example:

http://localhost:4200/dashboard/T64/27D I should navigate to a page with route T64/27D

Here's how I navigate this.router.navigate(['dashboard/'+this.groupListPage[0].code]); this.groupList[0].code contain T64/27D

Actually Angular separate T64 and 27D as 2 different pages.

Here's the error:

ERROR Error: Uncaught (in promise): Error: Cannot match any routes. URL Segment: 'dashboard/T64/27D'
Error: Cannot match any routes. URL Segment: 'dashboard/T64/27D'

How can I tell to Angular that / is a part of the param ?

2
  • What you can do is to get the 2 parts of the parameter in different variables, something like dashboard/:param1/:param2' in your state, and recompose it afterwards using param1 + '/' + param2. A workaround in a way, but it should work Commented May 18, 2018 at 9:05
  • The problem is that not always I have a slash .. Commented May 18, 2018 at 9:08

4 Answers 4

11

Assumming the route:

{
    path: 'dashboard/:id',
    component: FooComponent
 }

And :id can exist in {'abc','ab/c'}, in order to consider the inner '/' as part of the path, you need to use a custom UrlMatcher:

const customMatcher: UrlMatcher = (
  segments: UrlSegment[],
  group: UrlSegmentGroup,
  route: Route
): UrlMatchResult => {
  const { length } = segments;
  const firstSegment = segments[0];
  if (firstSegment.path === "dashboard" && length === 2 || length === 3) {
    // candidate for match
    const idSegments = segments
      .slice(1); // skip prefix
    const idPaths = idSegments.map(segment => segment.path);
    const mergedId = idPaths.join('/');// merge the splitted Id back together
    const idSegment: UrlSegment = new UrlSegment(mergedId, { id: mergedId });
    return ({ consumed: segments, posParams: { id: idSegment } });
  }
  return null;
};

A working example can be found in this blitz

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

3 Comments

I have discovered the UrlMatcher thanks to you, Nawsen response solved the issue and in my case it's simpler than UrlMatcher
It was hard to find really useful example how to map a parameter with slashes.Thank you a lot.
LOVE IT! Just 2 heads up for use: the customMatcher is hardcoded, modify 1) the path === "dashboard" to your application, and also 2) the parameter name in return(… { id: - I had used path/:tool instead of path/:id so had to rename to tool: idSegment
7

I don't think it is possible to do this with pathparams. It would perfectly work with queryparams tho.

You can also try to escape the slash with %2F, but I am not 100% sure how angular will take/parse this.

1 Comment

That's solved the issue, Angular parse well the param and transform automatically %2F to /
3

For anyone still looking for this, if you have special characters in your param you use navigate method in following way:

this.router.navigate(['dashboard', param]);

This way Angular will automatically escape special characters in param

Comments

-2
  You must define it in your routes.
  //if two param
  {
    path: 'dashboard/:id1/:id2',
    component: yourComponent
  }
  //if only one param
 {
     path: 'dashboard/:id1',
     component: yourComponent
 }
 {
     path: 'dashboard',
     component: yourComponent
 }
 and then navigate to your path
 this.router.navigate(['dashboard/'+this.groupListPage[0].code]);

2 Comments

A possible solution, but a poor one. Handling the recomposition of the id parameter in the same component is extra logic that you havent included.
the question was to identify the route path. So i gave the appropriate answer. The use Of ActivatedRoute to get the parameter value is extra logic, but i haven't included it since its not relevant to the question.

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.