Skip to content

Commit 9304608

Browse files
SkyZeroZxmmalerba
authored andcommitted
docs: add documentation for routerOutletData input and its usage (angular#64242)
PR Close angular#64242
1 parent fa75e4a commit 9304608

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

adev/src/content/guide/routing/show-routes-with-outlets.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,46 @@ You can add event listeners with the standard event binding syntax:
165165

166166
Check out the [API docs for RouterOutlet](/api/router/RouterOutlet?tab=api) if you’d like to learn more.
167167

168+
## Passing contextual data to routed components
169+
170+
Passing contextual data to routed components often requires global state or complicated route configurations. To make this easier, each `RouterOutlet` supports a `routerOutletData` input. Routed components and their children can read this data as a signal using the `ROUTER_OUTLET_DATA` injection token, allowing outlet-specific configuration without modifying route definitions.
171+
172+
```ts
173+
import { Component } from '@angular/core';
174+
import { RouterOutlet } from '@angular/router';
175+
176+
@Component({
177+
selector: 'app-dashboard',
178+
imports: [RouterOutlet],
179+
template: `
180+
<h2>Dashboard</h2>
181+
<router-outlet [routerOutletData]="{ layout: 'sidebar' }" />
182+
`,
183+
})
184+
export class DashboardComponent {}
185+
```
186+
187+
The routed component can inject the provided outlet data using `ROUTER_OUTLET_DATA`:
188+
189+
```ts
190+
import { Component, inject } from '@angular/core';
191+
import { ROUTER_OUTLET_DATA } from '@angular/router';
192+
193+
@Component({
194+
selector: 'app-stats',
195+
template: `<p>Stats view (layout: {{ outletData().layout }})</p>`,
196+
})
197+
export class StatsComponent {
198+
outletData = inject(ROUTER_OUTLET_DATA) as Signal<{ layout: string }>;
199+
}
200+
```
201+
202+
When Angular activates the `StatsComponent` in that outlet, it receives `{ layout: 'sidebar' }` as injected data.
203+
204+
NOTE: When the `routerOutletData` input is unset, the injected value is null by default.
205+
206+
---
207+
168208
## Next steps
169209

170210
Learn how to [navigate to routes](/guide/routing/navigate-to-routes) with Angular Router.

packages/router/src/directives/router_outlet.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ import {PRIMARY_OUTLET} from '../shared';
5555
* ```
5656
*
5757
* @publicApi
58+
* @see [Page routerOutletData](guide/routing/show-routes-with-outlets#passing-contextual-data-to-routed-components)
5859
*/
5960
export const ROUTER_OUTLET_DATA = new InjectionToken<Signal<unknown | undefined>>(
6061
typeof ngDevMode !== undefined && ngDevMode ? 'RouterOutlet data' : '',

0 commit comments

Comments
 (0)