13

Is it possible to use a partial-static parameter in angular 2 routing? I'm going to explain:

Now I'm using classic parameter like this:

const routes: Routes = [
  {
    path: ':type/fine.html',
    pathMatch: 'full',
    redirectTo: ':type/fine'
  }

But I would be able to use something like this:

const routes: Routes = [
      {
        path: 'static-:type/fine.html',
        pathMatch: 'full',
        redirectTo: 'static-:type/fine'
      }

To be able to redirect exactly to static-{{parameterValue}}/fine.

Is it possible?

2
  • 1
    You should look at angular.io/api/router/UrlMatcher. It allow to override the default matching strategy and provide your own. I think it may handle your case. Commented Mar 8, 2018 at 0:20
  • Just tried with that. The problem is that when the UrlMatcher tries to find :type parameter, it just sees that as a string, evaluating it exactly equal to ":type". Commented Mar 12, 2018 at 14:28

1 Answer 1

6

After many tries i've reached the knowledge that is impossible to do something like it. So i've implemented a completely different approach with a routing-guard following this example:

http://www.sparkbit.pl/angular-2-route-guards-real-life-example/

Just answering for anyone who will find same problem.

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

3 Comments

I believe this can be done with lazily loaded routed feature modules but it would be complex. Using guards seems like a solid alternative.
Already tried with lazy loaded modules but the problem is same: you can't match a partial static route. If you try you'll see that the only way to use a static&parametric routing path is to isolate a parameter between two / because angular sees a parameter only as a folder path or inside it.
I didn't realize that, that's a pretty serious limitation.

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.