Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting route params in modal components in angular

I can't get route params data in modal components.

Url: http://localhost:4200/portal/profiles/edit/5303

routing:

{ 
  path: ':accountId', 
  component: ProfileOverviewComponent, 
  data: { 
    title: 'Profile Overview', 
    icon: 'fa fa-id-card-o' 
  }
},

This command returns false:

if (this.route.snapshot.paramMap.get('accountId'))
like image 385
Bora Açıcı Avatar asked Mar 11 '26 07:03

Bora Açıcı


1 Answers

Modal components created with a named outlet are independent of the (unnamed) primary route but simply stacked on top. Would you need to get the route params (or any other data) from the primary route you can access it through the "root" property of the activated route.

You can do that like this:

const getLastChild = (route) => {
  let child = route;
  while (child.firstChild) {
    child = child.firstChild
  }
  return child;
}

const primary = this.route.snapshot.root; // Get the primary route
const lastChild = getLastChild(primary); // Get the last child (from which you want the params).
const params = lastChild.params;

This should resolve your problem, otherwise leave a comment.

like image 145
Wilt Avatar answered Mar 13 '26 01:03

Wilt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!