Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

222
Views
Angular - Dynamic routing with router param vaiable

I want to do something like this in my routing.module.ts (see lines marked by-->)

export const routes: Routes = [
        path: 'test/:action',
        component: CreateComponent,
        --> canActivate: ':action' == 'read' ? [Guard1] : [Guard1, Guard2],
        data: {
        -->  screenAction: ':action' == 'read' ? 'read' : ':action',
        }
]

I have to use the variable :action because it is used later in the router param. Thanks for your help !

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Well, what you ask is possible but implementing it might be a little complex.

First of all, you need to define multiple routes instead of 1. Where you can apply the conditions you require.

const routes: Routes = [
  { path: '', redirectTo: '/home', pathMatch: 'full' },
  { path: 'home', component: AppComponent },
  {
    path: 'dummy1/:action',
    component: Dummy1Component,
    canActivate: [GuardRotaterGuard],
  },
  {
    path: 'dummyx/:action',
    component: Dummy2Component,
    canActivate: [Guard1Guard],
  },
  {
    path: 'dummyz/:action',
    canActivate: [Guard1Guard, Guard2Guard],
    component: Dummy2Component,
  },
];

The route dummy1/:action is like a gateway route. Every request to dummy2 component should go from here. And then you need a decider/rotator guard that can decide and rotate the route depending on the route parameters. It should look like below :

canActivate(
    next: ActivatedRouteSnapshot,
    statex: RouterStateSnapshot
  ): Observable<boolean> | Promise<boolean> | boolean {
    const { action } = next.params;
    const state = { comesFrom: 'rotater' };
    console.log("rotater",action);
    if (action === 'read') { // you can decide whatever you want here
       this.router.navigate(['/dummyx/read'], { state });
    }else{
      this.router.navigate(['/dummyz', action], { state }); // pass the action to the other route as a parameter
    }

     return true;
  }

And here is a Stackblitz example in action.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!