Skip to content

Commit 626bf53

Browse files
committed
feat(cdk/dialog): expose rendered ComponentRef
Exposes the `ComponentRef` to the component that is rendered inside of a CDK dialog container. This allows users to interact with the component's inputs.
1 parent d5305f8 commit 626bf53

File tree

4 files changed

+12
-0
lines changed

4 files changed

+12
-0
lines changed

src/cdk/dialog/dialog-ref.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {Observable, Subject, Subscription} from 'rxjs';
1212
import {DialogConfig} from './dialog-config';
1313
import {FocusOrigin} from '@angular/cdk/a11y';
1414
import {BasePortalOutlet} from '@angular/cdk/portal';
15+
import {ComponentRef} from '@angular/core';
1516

1617
/** Additional options that can be passed in when closing a dialog. */
1718
export interface DialogCloseOptions {
@@ -29,6 +30,12 @@ export class DialogRef<R = unknown, C = unknown> {
2930
*/
3031
readonly componentInstance: C | null;
3132

33+
/**
34+
* `ComponentRef` of the component opened into the dialog. Will be
35+
* null when the dialog is opened using a `TemplateRef`.
36+
*/
37+
readonly componentRef: ComponentRef<C> | null;
38+
3239
/** Instance of the container that is rendering out the dialog content. */
3340
readonly containerInstance: BasePortalOutlet & {_closeInteractionType?: FocusOrigin};
3441

src/cdk/dialog/dialog.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
import {
1010
ChangeDetectionStrategy,
1111
Component,
12+
ComponentRef,
1213
Directive,
1314
inject,
1415
Inject,
@@ -89,6 +90,7 @@ describe('Dialog', () => {
8990

9091
expect(overlayContainerElement.textContent).toContain('Pizza');
9192
expect(dialogRef.componentInstance instanceof PizzaMsg).toBe(true);
93+
expect(dialogRef.componentRef instanceof ComponentRef).toBe(true);
9294
expect(dialogRef.componentInstance!.dialogRef).toBe(dialogRef);
9395

9496
viewContainerFixture.detectChanges();

src/cdk/dialog/dialog.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
Inject,
1717
Optional,
1818
SkipSelf,
19+
ComponentRef,
1920
} from '@angular/core';
2021
import {BasePortalOutlet, ComponentPortal, TemplatePortal} from '@angular/cdk/portal';
2122
import {of as observableOf, Observable, Subject, defer} from 'rxjs';
@@ -290,6 +291,7 @@ export class Dialog implements OnDestroy {
290291
config.componentFactoryResolver,
291292
),
292293
);
294+
(dialogRef as {componentRef: ComponentRef<C>}).componentRef = contentRef;
293295
(dialogRef as {componentInstance: C}).componentInstance = contentRef.instance;
294296
}
295297
}

tools/public_api_guard/cdk/dialog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ export class DialogRef<R = unknown, C = unknown> {
175175
close(result?: R, options?: DialogCloseOptions): void;
176176
readonly closed: Observable<R | undefined>;
177177
readonly componentInstance: C | null;
178+
readonly componentRef: ComponentRef<C> | null;
178179
// (undocumented)
179180
readonly config: DialogConfig<any, DialogRef<R, C>, BasePortalOutlet>;
180181
readonly containerInstance: BasePortalOutlet & {

0 commit comments

Comments
 (0)