Is it possible to gather all the params from an ActivatedRoute in angular? -


i'm creating , angular 4 application. happens need obtain parameters url @ different depth levels causing it's not uniform way in can obtain params.

suppose have url:

https://mysite/companies/:companyid/cups/:cupid/drawings/:drawid 

usually, according angular docs, need like:

ngoninit() { this.activatedroute.parent.parent.params.subscribe((params: params) =>  {this.companyid = params['companyid']; }); this.activatedroute.parent.params.subscribe((params: params) =>  {this.cupid = params['cupid']; }); this.activatedroute.params.subscribe((params: params) =>  {this.drawid = params['drawid']; }); } 

note considering inside drawdetailcomponent, meaning in component last child on hierarchy.

also please note depending on how deep in hierarchy, need add|subtract .parent activatedroute in correct position can params looking for.

so, possible have parameters urls same call?

something like:

this.activatedroute.parent.parent.params.subscribe((params: params) =>      {this.companyid = params['companyid'];     {this.cupid = params['cupid'];     {this.drawid = params['drawid']; }); 

we have uniform, not depth dependent way retrieving parameters our url.

actually tried like:

const property = 'companyid'; this.activatedroute.pathfromroot.foreach((item) => {   if (item.snapshot.params.hasownproperty(property)) {     item.params.subscribe((params: parammap) => {       this.companyid = params[property];     });   } }); 

but hope find better, more generic, testable way.

usually, in frameworks django, developer able gather information needed url in 1 single step. it's ok having subscriptions automatically handle changes on parameters in case of angular imho it's far being comfortable + testable.

i think should able this:

this.activatedroute.params.subscribe(params => {     this.companyid = params['companyid'];     this.cupid = params['cupid'];     this.drawid = params['drawid']; }); 

i have had luck similar in applications. notice not going parent. activated route should have need.

hope helps!


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -