You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
feat(router) add possibilty to subscribe to current ActivatedRoute #9662
[ ] bug report
[x ] feature request
[ ] support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question
Current behavior
Since #9390 is implemented we are now able to add a data-Object to the RouteConfig. This object is attached to the ActivatedRoute. As of now, I currently can't seem to find a way to globally subscribe to that ActivatedRoute to implement some generic functionallity like setting the browser title.
Expected/desired behavior
It would be super useful to be able to subscribe to the current ActivatedRoute or at least to be able to get this information ad-hoc from the router (via snapshot?!). Something like this would be quite nice:
The RouterState is a tree if ActivatedRoutes, so its already possible to get what you need although its not easy as what you've requested. You can subscribe to router events and map that to the routerState and to get the first activated route.
Thanks a lot for that trick @brandonroberts . I had to do some little changes for it to support child-routes, so if anyone is interested here is what I did:
@brandonroberts Hi, may you explain, please, how data observable prop shows up at the route? I try to do the same trick in my app, but data prop doesn't shows up...
@Niaro the data-field is not released yet. The plnkr uses the current master of angular. So if you want to use it in your app you currently need to build angular on your own.
In my opinion, the above plunkr is an overkill for such a simple thing we are trying to achieve here.
Correctly labelled as non-obvious though , I hope there will be a shortcut solution in the future Could we reopen this for providing a shortcut as a feature ?
should I open another issue or is this considered as final decision(no shorthands for this operation)?
Reacted by Umed Khudoiberdiev, Selim Arsever, Tsuyoshi Sumiyoshi, Shahmir Noorani, drnknmstrr, Gordon Sun, alexbyk, dan-madaket, Brian Frichette, Nik Karbaum and 6 more
still if you reload the page some information will be missing I will investigate further though and I will let you know. I guess it's because no all the routes are there as if you navigated to the age
As a side note the two approaches suggested earlier doesn't work if there are multiple active router outlets. I found that in those cases you only get the data for the primary outlet. And the Navigation event is only triggered once. I ended up with the code below which gives me an array of Data objects. Injected in the constructor are router: Router and activated: ActivatedRoute.
this.sub = router.events
.filter(event => event instanceof NavigationEnd)
.subscribe(() => {
let data: Data[] = [];
let activeRoutes: ActivatedRoute[] = activated.children;
activeRoutes.forEach((route: ActivatedRoute) => {
let activeRoute: ActivatedRoute = route;
while (activeRoute.firstChild) {
activeRoute = activeRoute.firstChild;
}
data.push(activeRoute.snapshot.data);
});
});
// do something with data
Reacted by rob gibson, pg2000, Nik Karbaum, Ben and Michael Spyratos
Any update ? 'firstChild' does not exist on type 'RouterState' anymore this is important if you had the shorthands you would keep that graph internally nothing would brake(eg. it would have the same interface).
Activity
brandonroberts commentedon Jun 28, 2016
The RouterState is a tree if ActivatedRoutes, so its already possible to get what you need although its not easy as what you've requested. You can subscribe to router events and map that to the routerState and to get the first activated route.
http://plnkr.co/edit/pHd89K?p=preview
choeller commentedon Jun 28, 2016
Thanks a lot for that trick @brandonroberts . I had to do some little changes for it to support child-routes, so if anyone is interested here is what I did:
http://plnkr.co/edit/rn5qh6?p=preview
Niaro commentedon Jun 29, 2016
@brandonroberts Hi, may you explain, please, how
dataobservable prop shows up at theroute? I try to do the same trick in my app, butdataprop doesn't shows up...choeller commentedon Jun 29, 2016
@Niaro the data-field is not released yet. The plnkr uses the current master of angular. So if you want to use it in your app you currently need to build angular on your own.
Niaro commentedon Jun 29, 2016
@choeller Okay...( Thank you)
DzmitryShylovich commentedon Jun 29, 2016
or u can use https://github.com/angular/router-builds
delagen commentedon Jul 7, 2016
+1
vsavkin commentedon Jul 28, 2016
As Brandon pointed out the way to achieve this is to do something like this:
Closing the issue.
agalazis commentedon Jul 29, 2016
In my opinion, the above plunkr is an overkill for such a simple thing we are trying to achieve here.
Correctly labelled as non-obvious though , I hope there will be a shortcut solution in the future Could we reopen this for providing a shortcut as a feature ?
should I open another issue or is this considered as final decision(no shorthands for this operation)?
tsu1980 commentedon Aug 6, 2016
What about when page reloaded?
Is NavigationEnd triggered when page reloaded?
AndersAppelt commentedon Aug 18, 2016
@tsu1980 Yes all three, NavigationStart, RoutesRecognized and NavigationEnd are triggered when the page loads.
agalazis commentedon Aug 21, 2016
still if you reload the page some information will be missing I will investigate further though and I will let you know. I guess it's because no all the routes are there as if you navigated to the age
AndersAppelt commentedon Aug 22, 2016
As a side note the two approaches suggested earlier doesn't work if there are multiple active router outlets. I found that in those cases you only get the data for the primary outlet. And the Navigation event is only triggered once. I ended up with the code below which gives me an array of Data objects. Injected in the constructor are
router: Routerandactivated: ActivatedRoute.agalazis commentedon Sep 29, 2016
Any update ? 'firstChild' does not exist on type 'RouterState' anymore this is important if you had the shorthands you would keep that graph internally nothing would brake(eg. it would have the same interface).
brandonroberts commentedon Sep 29, 2016
@agalazis
firstChildexists on eachActivatedRoutenow. TheRouterStateno longer handles traversing the route tree directlysunshineo commentedon Oct 5, 2016
So how do we fix something like this now:
alexbyk commentedon Oct 7, 2016
If someone is looking for working solution, here it is, based on previous examples (but never blindly trust code snippets from the internet)
https://gist.github.com/alexbyk/0801373112b64bd171721c4b6b23e9e2
@sunshineo this might help you
sunshineo commentedon Oct 7, 2016
@alexbyk Forwarding to a project where this kind of code is used.
gitnik commentedon Nov 16, 2016
Can this be reopened? The provided official solution does not work for multiple outlets and is anything but nice to use.
avinashjoshi commentedon Jan 18, 2017
This is a great answer: https://toddmotto.com/dynamic-page-titles-angular-2-router-events
delagen commentedon Jul 7, 2017
@KSuttle-cng update title on route change from route data
mickaelb1 commentedon Dec 4, 2017
Similar fix:
https://github.com/stomo21/ng-router-state-params
angular-automatic-lock-bot commentedon Sep 12, 2019
This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.