Skip to content

Commit

Permalink
fix(portal): cleanup PortalHost on directive destroy (#1703)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewseguin authored and jelbourn committed Nov 3, 2016
1 parent 8ce65ca commit 7e08468
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/lib/core/portal/portal-directives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import {
Directive,
TemplateRef,
ComponentFactoryResolver,
ViewContainerRef
ViewContainerRef,
OnDestroy
} from '@angular/core';
import {Portal, TemplatePortal, ComponentPortal, BasePortalHost} from './portal';

Expand Down Expand Up @@ -41,7 +42,7 @@ export class TemplatePortalDirective extends TemplatePortal {
selector: '[portalHost]',
inputs: ['portal: portalHost']
})
export class PortalHostDirective extends BasePortalHost {
export class PortalHostDirective extends BasePortalHost implements OnDestroy {
/** The attached portal. */
private _portal: Portal<any>;

Expand All @@ -59,7 +60,11 @@ export class PortalHostDirective extends BasePortalHost {
this._replaceAttachedPortal(p);
}

/** Attach the given ComponentPortal to this PortlHost using the ComponentFactoryResolver. */
ngOnDestroy() {
this.dispose();
}

/** Attach the given ComponentPortal to this PortalHost using the ComponentFactoryResolver. */
attachComponentPortal<T>(portal: ComponentPortal<T>): ComponentRef<T> {
portal.setAttachedHost(this);

Expand Down
12 changes: 12 additions & 0 deletions src/lib/core/portal/portal.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,18 @@ describe('Portals', () => {
expect(hostContainer.textContent).toContain('Pizza');
});

it('should dispose the host when destroyed', () => {
// Set the selectedHost to be a ComponentPortal.
let testAppComponent = fixture.debugElement.componentInstance;
testAppComponent.selectedPortal = new ComponentPortal(PizzaMsg);

fixture.detectChanges();
expect(testAppComponent.selectedPortal.isAttached).toBe(true);

fixture.destroy();
expect(testAppComponent.selectedPortal.isAttached).toBe(false);
});

it('should load a component into the portal with a given injector', () => {
// Create a custom injector for the component.
let chocolateInjector = new ChocolateInjector(fixture.componentInstance.injector);
Expand Down

0 comments on commit 7e08468

Please sign in to comment.