Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(portals): dispose portalhost when destroyed #1703

Merged
merged 1 commit into from
Nov 3, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -42,6 +42,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