40

I am having URLs like

  • www.yoursite.com/accounts/:accountid/info
  • www.yoursite.com/accounts/:accountid/users etc.

An integer value accountid is available in the URL

But can't access it using the ActivatedRoute parameters.

Now I am splitting the URL to get the accountid

Is there a better way to access the value other than splitting the URL ?

Update

I have a different module called account which includes all components and services for account related pages.

I am lazy loading this module. So in the root level routing , that is app-routing I specify

{ path:'account', loadChildren : './account/account.module#AccountModule' }

In the account module's account-routing, I specify the children by

path: 'account/:accountid', component: AccountComponent, canActivate: [AuthGuard],
    children: [
      { path: '', redirectTo: 'info', pathMatch: 'full' },
      { path: 'info', component: InfoComponent },
      { path: 'users, component: UsersComponent }
      {path: '**', redirectTo: 'info'}
    ]

Note :

We can access the params in children path if present. But not of parent part why?

I am getting undefined for params in parent path

jophab
  • 5,356
  • 14
  • 41
  • 60

10 Answers10

89

With the help of this article, the problem was solved.

For accessing the params in parent route, we need to use activeRoute.parent.params instead of activeRoute.params

this.activatedRoute.parent.params.subscribe(
    (params) => 
    { 
                 console.log(params.accountid); 
     });

Also from this stackoverflow question , a solution to make all params (including parent route params) available to child routes was found.

This is done by including a configuration object with the paramsInheritanceStrategy set to always where we import RouterModule

eg :

import {RouterModule, ExtraOptions} from "@angular/router";

export const routingConfiguration: ExtraOptions = {
  paramsInheritanceStrategy: 'always'
};

export const Routing = RouterModule.forRoot(routes, routingConfiguration);

Now we can access both parent and child route params using activeRoute.params

jophab
  • 5,356
  • 14
  • 41
  • 60
15

All you need is to use paramsInheritanceStrategy. See https://angular.io/api/router/ExtraOptions

Vugar Abdullayev
  • 1,852
  • 3
  • 21
  • 46
8

Try this solution:

 this.activatedRoute.params.subscribe(params => {
        const accountid= params['accountid'];
    });

You can also try this if above didn't work. (Got this solution from here)

this.router.parent.params.switchMap(params => {
  // params = { id: 1234 }
});
Seba Cherian
  • 1,755
  • 6
  • 18
  • Thanks for the answer. I have already tried this. Not working this case. I have updated the question to include more details. – jophab Mar 29 '19 at 06:32
  • @Seba Cherian here ,accountid is 'route param' not 'query param' so i think your solution won't work to get accountid value. – Nirali Mar 29 '19 at 07:12
  • If this answer solved your problem consider marking this answer/answer that worked as accepted(tick the answer) – Seba Cherian Mar 29 '19 at 08:27
  • Starting from Angular 5 you can use paramsInheritanceStrategy instead. No need to subscribe to parent route. – Vugar Abdullayev Aug 06 '21 at 08:33
4

The parent property has been added to the route now:

 this.route.parent.paramMap
                .subscribe(params => {
                    if (params.has('accountid')) {
                        this.'accountid'= params.get('accountid');
                    }
                });
matt_lethargic
  • 2,706
  • 1
  • 18
  • 33
1

Try this

create the object of ActivatedRoute in constructor

constructor(private route: ActivatedRoute) {}

so once object get created you will get route parameters like

    let accountid = this.route.snapshot.params['accountid'];

    console.log(accountid);

OR

let accountid = this.activatedRoute.snapshot.paramMap.get('accountid');
console.log(accountid);

I hope this will be useful.

Sandy Sanap
  • 755
  • 1
  • 5
  • 17
1

private route: ActivatedRoute

this.route.snapshot.parent.params['accountid'])

Oscar
  • 31
  • 3
0

if you're doing a standalone main.ts without app.modules, you can use the withRouterConfig

bootstrapApplication(AppComponent, {
  providers: [
    // https://this-is-angular.github.io/angular-guides/docs/standalone-apis/configuring-the-router-using-standalone-features
    provideRouter(routes,
      withRouterConfig({paramsInheritanceStrategy: 'always'})
    ),
    ...
Rusty Rob
  • 16,489
  • 8
  • 100
  • 116
0

I need to get params all the way from app-routing so this worked for me

this.route.firstChild?.params.subscribe((param) => {
      const accountId = param['account-id'];
});

Happy Coding!

Bhavin
  • 970
  • 1
  • 13
  • 20
0

for my case I had to go up several parents

this._activatedRoute.parent.parent.snapshot.paramMap.get('id')
Johansrk
  • 5,160
  • 3
  • 38
  • 37
-1

I think you need to access the current route params through following code in your constructor,

this.activeRoute.params.subscribe(params => {
     if(params['accountid']) {
         console.log(params['accountid'])  // use it where you need
     }
})
Prashant Pimpale
  • 10,349
  • 9
  • 44
  • 84
Nirali
  • 454
  • 1
  • 6
  • 16