Skip to content

Commit 30c3e13

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

File tree

4 files changed

+13
-0
lines changed

4 files changed

+13
-0
lines changed

src/material/dialog/dialog-ref.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {_MatDialogContainerBase} from './dialog-container';
1515
import {filter, take} from 'rxjs/operators';
1616
import {ESCAPE, hasModifierKey} from '@angular/cdk/keycodes';
1717
import {GlobalPositionStrategy} from '@angular/cdk/overlay';
18+
import {ComponentRef} from '@angular/core';
1819

1920
export const enum MatDialogState {
2021
OPEN,
@@ -29,6 +30,12 @@ export class MatDialogRef<T, R = any> {
2930
/** The instance of component opened into the dialog. */
3031
componentInstance: T;
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<T> | null;
38+
3239
/** Whether the user is allowed to close the dialog. */
3340
disableClose: boolean | undefined;
3441

src/material/dialog/dialog.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
ChangeDetectionStrategy,
1818
Component,
1919
ComponentFactoryResolver,
20+
ComponentRef,
2021
createNgModuleRef,
2122
Directive,
2223
Inject,
@@ -110,6 +111,7 @@ describe('MDC-based MatDialog', () => {
110111

111112
expect(overlayContainerElement.textContent).toContain('Pizza');
112113
expect(dialogRef.componentInstance instanceof PizzaMsg).toBe(true);
114+
expect(dialogRef.componentRef instanceof ComponentRef).toBe(true);
113115
expect(dialogRef.componentInstance.dialogRef).toBe(dialogRef);
114116

115117
viewContainerFixture.detectChanges();

src/material/dialog/dialog.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {ComponentType, Overlay, OverlayContainer, ScrollStrategy} from '@angular
1010
import {Location} from '@angular/common';
1111
import {
1212
ANIMATION_MODULE_TYPE,
13+
ComponentRef,
1314
Inject,
1415
Injectable,
1516
InjectionToken,
@@ -197,6 +198,7 @@ export abstract class _MatDialogBase<C extends _MatDialogContainerBase> implemen
197198

198199
// This can't be assigned in the `providers` callback, because
199200
// the instance hasn't been assigned to the CDK ref yet.
201+
(dialogRef! as {componentRef: ComponentRef<T>}).componentRef = cdkRef.componentRef!;
200202
dialogRef!.componentInstance = cdkRef.componentInstance!;
201203

202204
this.openDialogs.push(dialogRef!);

tools/public_api_guard/material/dialog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import { AnimationTriggerMetadata } from '@angular/animations';
88
import { CdkDialogContainer } from '@angular/cdk/dialog';
99
import { ComponentFactoryResolver } from '@angular/core';
10+
import { ComponentRef } from '@angular/core';
1011
import { ComponentType } from '@angular/cdk/overlay';
1112
import { DialogRef } from '@angular/cdk/dialog';
1213
import { Direction } from '@angular/cdk/bidi';
@@ -250,6 +251,7 @@ export class MatDialogRef<T, R = any> {
250251
beforeClosed(): Observable<R | undefined>;
251252
close(dialogResult?: R): void;
252253
componentInstance: T;
254+
readonly componentRef: ComponentRef<T> | null;
253255
// (undocumented)
254256
_containerInstance: _MatDialogContainerBase;
255257
disableClose: boolean | undefined;

0 commit comments

Comments
 (0)