Skip to content

Commit 49d279c

Browse files
committed
feat(portal): expose attached result in CdkPortalOutlet
Exposes the attach `ComponentRef` or `EmbeddedViewRef` in the `CdkPortalOutlet` directive. Fixes #9304.
1 parent 303e004 commit 49d279c

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

src/cdk/portal/portal-directives.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ export class CdkPortalOutlet extends BasePortalOutlet implements OnInit, OnDestr
5252
/** Whether the portal component is initialized. */
5353
private _isInitialized = false;
5454

55+
/** Reference to the currently-attached component/view ref. */
56+
private _result: ComponentRef<any> | EmbeddedViewRef<any> | null;
57+
5558
constructor(
5659
private _componentFactoryResolver: ComponentFactoryResolver,
5760
private _viewContainerRef: ViewContainerRef) {
@@ -93,13 +96,19 @@ export class CdkPortalOutlet extends BasePortalOutlet implements OnInit, OnDestr
9396
this._attachedPortal = portal;
9497
}
9598

99+
/** Component or view reference that is attached to the portal. */
100+
get result(): ComponentRef<any> | EmbeddedViewRef<any> | null {
101+
return this._result;
102+
}
103+
96104
ngOnInit() {
97105
this._isInitialized = true;
98106
}
99107

100108
ngOnDestroy() {
101109
super.dispose();
102110
this._attachedPortal = null;
111+
this._result = null;
103112
}
104113

105114
/**
@@ -125,6 +134,7 @@ export class CdkPortalOutlet extends BasePortalOutlet implements OnInit, OnDestr
125134

126135
super.setDisposeFn(() => ref.destroy());
127136
this._attachedPortal = portal;
137+
this._result = ref;
128138

129139
return ref;
130140
}
@@ -140,6 +150,7 @@ export class CdkPortalOutlet extends BasePortalOutlet implements OnInit, OnDestr
140150
super.setDisposeFn(() => this._viewContainerRef.clear());
141151

142152
this._attachedPortal = portal;
153+
this._result = viewRef;
143154

144155
return viewRef;
145156
}

src/cdk/portal/portal.spec.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import {
1010
Optional,
1111
Injector,
1212
ApplicationRef,
13-
TemplateRef
13+
TemplateRef,
14+
ComponentRef,
1415
} from '@angular/core';
1516
import {CommonModule} from '@angular/common';
1617
import {CdkPortal, CdkPortalOutlet, PortalModule} from './portal-directives';
@@ -45,6 +46,7 @@ describe('Portals', () => {
4546
// Expect that the content of the attached portal is present.
4647
expect(hostContainer.textContent).toContain('Pizza');
4748
expect(testAppComponent.portalOutlet.portal).toBe(componentPortal);
49+
expect(testAppComponent.portalOutlet.result instanceof ComponentRef).toBe(true);
4850
});
4951

5052
it('should load a template into the portal', () => {
@@ -58,6 +60,11 @@ describe('Portals', () => {
5860
// Expect that the content of the attached portal is present and no context is projected
5961
expect(hostContainer.textContent).toContain('Banana');
6062
expect(testAppComponent.portalOutlet.portal).toBe(templatePortal);
63+
64+
// We can't test whether it's an instance of an `EmbeddedViewRef` so
65+
// we verify that it's defined and that it's not a ComponentRef.
66+
expect(testAppComponent.portalOutlet.result instanceof ComponentRef).toBe(false);
67+
expect(testAppComponent.portalOutlet.result).toBeTruthy();
6168
});
6269

6370
it('should project template context bindings in the portal', () => {

0 commit comments

Comments
 (0)