Skip to content

Commit 2bd764a

Browse files
crisbetothePunderWoman
authored andcommitted
feat(router): allow router outlet to be set on ng-container (#64562)
Updates the selector for `RouterOutlet` to allow for it to be set on an `ng-container`. This allows it to not render the host node which can affect the layout. Fixes #64553. PR Close #64562
1 parent 3d9837b commit 2bd764a

3 files changed

Lines changed: 27 additions & 2 deletions

File tree

‎goldens/public-api/router/index.api.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -901,7 +901,7 @@ export class RouterOutlet implements OnDestroy, OnInit, RouterOutletContract {
901901
readonly routerOutletData: i0.InputSignal<unknown>;
902902
readonly supportsBindingToComponentInputs = true;
903903
// (undocumented)
904-
static ɵdir: i0.ɵɵDirectiveDeclaration<RouterOutlet, "router-outlet", ["outlet"], { "name": { "alias": "name"; "required": false; }; "routerOutletData": { "alias": "routerOutletData"; "required": false; "isSignal": true; }; }, { "activateEvents": "activate"; "deactivateEvents": "deactivate"; "attachEvents": "attach"; "detachEvents": "detach"; }, never, never, true, never>;
904+
static ɵdir: i0.ɵɵDirectiveDeclaration<RouterOutlet, "router-outlet, ng-container[routerOutlet]", ["outlet"], { "name": { "alias": "name"; "required": false; }; "routerOutletData": { "alias": "routerOutletData"; "required": false; "isSignal": true; }; }, { "activateEvents": "activate"; "deactivateEvents": "deactivate"; "attachEvents": "attach"; "detachEvents": "detach"; }, never, never, true, never>;
905905
// (undocumented)
906906
static ɵfac: i0.ɵɵFactoryDeclaration<RouterOutlet, never>;
907907
}

‎packages/router/src/directives/router_outlet.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ export interface RouterOutletContract {
204204
* @publicApi
205205
*/
206206
@Directive({
207-
selector: 'router-outlet',
207+
selector: 'router-outlet, ng-container[routerOutlet]',
208208
exportAs: 'outlet',
209209
})
210210
export class RouterOutlet implements OnDestroy, OnInit, RouterOutletContract {

‎packages/router/test/directives/router_outlet.spec.ts‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,31 @@ describe('router outlet name', () => {
194194
// Not contain because route was changed back to parent
195195
expect(fixture.nativeElement.innerHTML).not.toContain('child component');
196196
});
197+
198+
it('should be able to use a router outlet on an ng-container', async () => {
199+
@Component({
200+
template: '<ng-container routerOutlet [name]="name"/>',
201+
imports: [RouterOutlet],
202+
})
203+
class RootCmp {
204+
name = 'popup';
205+
}
206+
207+
@Component({
208+
template: 'popup component',
209+
})
210+
class PopupCmp {}
211+
212+
TestBed.configureTestingModule({
213+
imports: [RouterModule.forRoot([{path: '', outlet: 'popup', component: PopupCmp}])],
214+
});
215+
const router = TestBed.inject(Router);
216+
const fixture = await createRoot(router, RootCmp);
217+
218+
expect(fixture.nativeElement.innerHTML.trim()).toBe(
219+
'<ng-component>popup component</ng-component><!--ng-container-->',
220+
);
221+
});
197222
});
198223

199224
describe('component input binding', () => {

0 commit comments

Comments
 (0)
Sponsor
SponsoredKunjungi sekarang
Promo