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
Which @angular/* package(s) are relevant/related to the feature request?
router
Description
Since Angular 14, router has a new mechanism to update page title from route config. The route can either be a static string or resolved in the same way as resolve data.
But the resolved route title is not exposed and inaccessible from client application (it seems the title is actually stored in the resolved Data, but using a private Symbol so it can't be accessed properly).
Proposed solution
Expose a new title property on ActivatedRoute and ActivatedRouteSnapshot, just like existing params, data...
Alternatives considered
Alternative 1: Expose the internal RouteTitle symbol used to store title in Data, but this is probably not wanted because it would expose an implementation detail.
Alternative 2: Use TitleStrategy.buildTitle(snapshot: RouterStateSnapshot) or, better, TitleStrategy.getResolvedTitleForRoute(snapshot: ActivatedRouteSnapshot). But this has following drawbacks:
TitleStrategy must be injected where we need to (only) read the route title, which is not its primary purpose...
TitleStrategy is not very convenient to inject, because in default case, DefaultTitleStrategy is used but is not available through DI with TitleStrategy token. To get the actual strategy used, one must retrieve it from Router.titleStrategy.
This is inconsistent with existing public api of ActivatedRoute/ActivatedRouteSnapshot
@EmmanuelRoux Thanks for the request. Can you describe your use-case for needing the resolved title on each individual ActivatedRoute? Given that the browser title is effectively a singleton, what's the use-case for looking at each individually resolved title?
I don't agree with the first two bullet points of your argument but can certainly get behind the third. For me, the third point is a good enough reason to move forward with the change.
P.S. Changing the title strategy should be done by providing your own through DI, not directly modifying the property on the Router. Unfortunately, the property was exposed in order to match the other strategy patterns used. I'll make a note to investigate making all of these private as a breaking change in v15.
This feature request is now candidate for our backlog! In the next phase, the community has 60 days to upvote. If the request receives more than 20 upvotes, we'll move it to our consideration list.
You can find more details about the feature request process in our documentation.
My use case is specific to a lib which needs to retrieve the title defined for each pages, but I think it may apply to many projects.
Given that the browser title is effectively a singleton, what's the use-case for looking at each individually resolved title?
I understand and totally agree with you, but despite DefaultTitleStrategy will update the document title, there is actually absolutely no guarantee the user-chosen strategy will.
P.S. Changing the title strategy should be done by providing your own through DI, not directly modifying the property on the Router. Unfortunately, the property was exposed in order to match the other strategy patterns used. I'll make a note to investigate making all of these private as a breaking change in v15.
That's not exactly what I tried to explain: I just describe how to retrieve the actual title strategy (because it could not be retrieved via DI in case the default one is used - if I understand correctly).
I understand and totally agree with you, but despite DefaultTitleStrategy will update the document title, there is actually absolutely no guarantee the user-chosen strategy will.
That's not exactly what I tried to explain: I just describe how to retrieve the actual title strategy (because it could not be retrieved via DI in case the default one is used - if I understand correctly).
Unless I'm still not understanding your explanation, what you're saying is not true. The router uses the DefaultTitleStrategy by default only. You can provide your own title strategy using {provide: TitleStrategy, useClass: MyCustomStrategy} and that's the one that's used. https://stackblitz.com/edit/angular-ivy-kslayx
I'm speaking here as a library author, so I have no control on the strategy the app developer will define in the app.
That said, at some point I need to know what title has been defined for a given ActivatedRoute.
What I'm saying is: in order to retrieve the title defined by the app developer, there are several choices but none of them seems good for me:
I can read in the route config, but because the title may be a Resolve<string> this is not easy
I can inject the TitleStrategy and call getResolvedTitleForRoute, just like your example: but this will not work if the user is using the default strategy, except by using something like this: inject(TitleStrategy, InjectFlags.Optional) || inject(DefaultTitleStrategy)
I can read the document title (either from DOM or by using Title service): but currently nothing guarantees that a custom title strategy will actually update document title (although it is surely not a very common scenario)
@EmmanuelRoux Thanks, I understand now. I appreciate the detailed explanation. It helps a lot to justify why we make the change. Even though it's just a 1 liner in each of the ActivatedRoute and ActivatedRouteSnapshot, it's still good to have concrete reasoning.
Activity
atscott commentedon Jul 13, 2022
@EmmanuelRoux Thanks for the request. Can you describe your use-case for needing the resolved title on each individual
ActivatedRoute? Given that the browser title is effectively a singleton, what's the use-case for looking at each individually resolved title?I don't agree with the first two bullet points of your argument but can certainly get behind the third. For me, the third point is a good enough reason to move forward with the change.
P.S. Changing the title strategy should be done by providing your own through DI, not directly modifying the property on the Router. Unfortunately, the property was exposed in order to match the other strategy patterns used. I'll make a note to investigate making all of these private as a breaking change in v15.
angular-robot commentedon Jul 14, 2022
This feature request is now candidate for our backlog! In the next phase, the community has 60 days to upvote. If the request receives more than 20 upvotes, we'll move it to our consideration list.
You can find more details about the feature request process in our documentation.
EmmanuelRoux commentedon Jul 15, 2022
Thanks for the feedback!
My use case is specific to a lib which needs to retrieve the title defined for each pages, but I think it may apply to many projects.
I understand and totally agree with you, but despite
DefaultTitleStrategywill update the document title, there is actually absolutely no guarantee the user-chosen strategy will.That's not exactly what I tried to explain: I just describe how to retrieve the actual title strategy (because it could not be retrieved via DI in case the default one is used - if I understand correctly).
atscott commentedon Jul 16, 2022
Unless I'm still not understanding your explanation, what you're saying is not true. The router uses the
DefaultTitleStrategyby default only. You can provide your own title strategy using{provide: TitleStrategy, useClass: MyCustomStrategy}and that's the one that's used. https://stackblitz.com/edit/angular-ivy-kslayxEmmanuelRoux commentedon Jul 16, 2022
Sorry I was not clear!
I'm speaking here as a library author, so I have no control on the strategy the app developer will define in the app.
That said, at some point I need to know what title has been defined for a given
ActivatedRoute.What I'm saying is: in order to retrieve the title defined by the app developer, there are several choices but none of them seems good for me:
Resolve<string>this is not easyTitleStrategyand callgetResolvedTitleForRoute, just like your example: but this will not work if the user is using the default strategy, except by using something like this:inject(TitleStrategy, InjectFlags.Optional) || inject(DefaultTitleStrategy)Titleservice): but currently nothing guarantees that a custom title strategy will actually update document title (although it is surely not a very common scenario)atscott commentedon Jul 18, 2022
@EmmanuelRoux Thanks, I understand now. I appreciate the detailed explanation. It helps a lot to justify why we make the change. Even though it's just a 1 liner in each of the
ActivatedRouteandActivatedRouteSnapshot, it's still good to have concrete reasoning.angular-robot commentedon Aug 23, 2022
Just a heads up that we kicked off a community voting process for your feature request. There are 20 days until the voting process ends.
Find more details about Angular's feature request process in our documentation.
EmmanuelRoux commentedon Aug 30, 2022
Fixed by #46826
angular-automatic-lock-bot commentedon Sep 30, 2022
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.