Skip to content

Commit fa75e4a

Browse files
SkyZeroZxmmalerba
authored andcommitted
docs: Adds guide for customizing page titles with TitleStrategy (angular#64238)
Improves documentation by explaining how to implement and configure a custom title strategy for centralized control over page titles PR Close angular#64238
1 parent 1ffc643 commit fa75e4a

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

adev/src/content/guide/routing/define-routes.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,44 @@ const routes: Routes = [
299299

300300
Route titles can also be set via a service extending the [`TitleStrategy`](/api/router/TitleStrategy) abstract class. By default, Angular uses the [`DefaultTitleStrategy`](/api/router/DefaultTitleStrategy).
301301

302+
### Using TitleStrategy for page titles
303+
304+
For advanced scenarios where you need centralized control over how the document title is composed, implement a `TitleStrategy`.
305+
306+
`TitleStrategy` is a token you can provide to override the default title strategy used by Angular. You can supply a custom `TitleStrategy` to implement conventions such as adding an application suffix, formatting titles from breadcrumbs, or generating titles dynamically from route data.
307+
308+
```ts
309+
import { Injectable } from '@angular/core';
310+
import { Title } from '@angular/platform-browser';
311+
import { TitleStrategy, RouterStateSnapshot } from '@angular/router';
312+
313+
@Injectable()
314+
export class AppTitleStrategy extends TitleStrategy {
315+
private readonly title = inject(Title);
316+
317+
updateTitle(snapshot: RouterStateSnapshot): void {
318+
// PageTitle is equal to the "Title" of a route if it's set
319+
// If its not set it will use the "title" given in index.html
320+
const pageTitle = this.buildTitle(snapshot) || this.title.getTitle();
321+
this.title.setTitle(`MyAwesomeApp - ${pageTitle}`);
322+
}
323+
}
324+
```
325+
326+
To use the custom strategy, provide it with the `TitleStrategy` token at the application level:
327+
328+
```ts
329+
import { provideRouter, TitleStrategy } from '@angular/router';
330+
import { AppTitleStrategy } from './app-title.strategy';
331+
332+
export const appConfig = {
333+
providers: [
334+
provideRouter(routes),
335+
{ provide: TitleStrategy, useClass: AppTitleStrategy },
336+
],
337+
};
338+
```
339+
302340
## Route-level providers for dependency injection
303341

304342
Each route has a `providers` property that lets you provide dependencies to that route's content via [dependency injection](/guide/di).

packages/router/src/page_title_strategy.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import {PRIMARY_OUTLET, RouteTitleKey} from './shared';
3333
* incorporate titles in named outlets.
3434
*
3535
* @publicApi
36-
* @see [Page title guide](guide/routing/common-router-tasks#setting-the-page-title)
36+
* @see [Page title guide](guide/routing/define-routes#using-titlestrategy-for-page-titles)
3737
*/
3838
@Injectable({providedIn: 'root', useFactory: () => inject(DefaultTitleStrategy)})
3939
export abstract class TitleStrategy {

0 commit comments

Comments
 (0)