Skip to content

Commit

Permalink
feat(portal): expose attached result in CdkPortalOutlet
Browse files Browse the repository at this point in the history
Exposes the attach `ComponentRef` or `EmbeddedViewRef` in the `CdkPortalOutlet` directive.

Fixes angular#9304.
  • Loading branch information
crisbeto committed Jan 10, 2018
1 parent 303e004 commit 49d279c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
11 changes: 11 additions & 0 deletions src/cdk/portal/portal-directives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ export class CdkPortalOutlet extends BasePortalOutlet implements OnInit, OnDestr
/** Whether the portal component is initialized. */
private _isInitialized = false;

/** Reference to the currently-attached component/view ref. */
private _result: ComponentRef<any> | EmbeddedViewRef<any> | null;

constructor(
private _componentFactoryResolver: ComponentFactoryResolver,
private _viewContainerRef: ViewContainerRef) {
Expand Down Expand Up @@ -93,13 +96,19 @@ export class CdkPortalOutlet extends BasePortalOutlet implements OnInit, OnDestr
this._attachedPortal = portal;
}

/** Component or view reference that is attached to the portal. */
get result(): ComponentRef<any> | EmbeddedViewRef<any> | null {
return this._result;
}

ngOnInit() {
this._isInitialized = true;
}

ngOnDestroy() {
super.dispose();
this._attachedPortal = null;
this._result = null;
}

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

super.setDisposeFn(() => ref.destroy());
this._attachedPortal = portal;
this._result = ref;

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

this._attachedPortal = portal;
this._result = viewRef;

return viewRef;
}
Expand Down
9 changes: 8 additions & 1 deletion src/cdk/portal/portal.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import {
Optional,
Injector,
ApplicationRef,
TemplateRef
TemplateRef,
ComponentRef,
} from '@angular/core';
import {CommonModule} from '@angular/common';
import {CdkPortal, CdkPortalOutlet, PortalModule} from './portal-directives';
Expand Down Expand Up @@ -45,6 +46,7 @@ describe('Portals', () => {
// Expect that the content of the attached portal is present.
expect(hostContainer.textContent).toContain('Pizza');
expect(testAppComponent.portalOutlet.portal).toBe(componentPortal);
expect(testAppComponent.portalOutlet.result instanceof ComponentRef).toBe(true);
});

it('should load a template into the portal', () => {
Expand All @@ -58,6 +60,11 @@ describe('Portals', () => {
// Expect that the content of the attached portal is present and no context is projected
expect(hostContainer.textContent).toContain('Banana');
expect(testAppComponent.portalOutlet.portal).toBe(templatePortal);

// We can't test whether it's an instance of an `EmbeddedViewRef` so
// we verify that it's defined and that it's not a ComponentRef.
expect(testAppComponent.portalOutlet.result instanceof ComponentRef).toBe(false);
expect(testAppComponent.portalOutlet.result).toBeTruthy();
});

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

0 comments on commit 49d279c

Please sign in to comment.