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(portal-host): unable to clear and portal reference not being set #3302

Merged
merged 1 commit into from
Mar 20, 2017
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
29 changes: 14 additions & 15 deletions src/lib/core/portal/portal-directives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,21 @@ export class PortalHostDirective extends BasePortalHost implements OnDestroy {
return this._portal;
}

set portal(p: Portal<any>) {
if (p) {
this._replaceAttachedPortal(p);
set portal(portal: Portal<any>) {
if (this.hasAttached()) {
super.detach();
}

if (portal) {
super.attach(portal);
}

this._portal = portal;
}

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

/**
Expand All @@ -94,6 +101,8 @@ export class PortalHostDirective extends BasePortalHost implements OnDestroy {
portal.injector || viewContainerRef.parentInjector);

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

return ref;
}

Expand All @@ -107,21 +116,11 @@ export class PortalHostDirective extends BasePortalHost implements OnDestroy {
this._viewContainerRef.createEmbeddedView(portal.templateRef);
super.setDisposeFn(() => this._viewContainerRef.clear());

this._portal = portal;

// TODO(jelbourn): return locals from view
return new Map<string, any>();
}

/** Detaches the currently attached Portal (if there is one) and attaches the given Portal. */
private _replaceAttachedPortal(p: Portal<any>): void {
if (this.hasAttached()) {
super.detach();
}

if (p) {
super.attach(p);
this._portal = p;
}
}
}


Expand Down
50 changes: 49 additions & 1 deletion src/lib/core/portal/portal.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {inject, ComponentFixture, TestBed, async} from '@angular/core/testing';
import {
NgModule,
Component,
ViewChild,
ViewChildren,
QueryList,
ViewContainerRef,
Expand All @@ -10,7 +11,7 @@ import {
Injector,
ApplicationRef,
} from '@angular/core';
import {TemplatePortalDirective, PortalModule} from './portal-directives';
import {TemplatePortalDirective, PortalHostDirective, PortalModule} from './portal-directives';
import {Portal, ComponentPortal} from './portal';
import {DomPortalHost} from './dom-portal-host';

Expand Down Expand Up @@ -141,6 +142,52 @@ describe('Portals', () => {

expect(hostContainer.textContent).toContain('Pizza');
});

it('should detach the portal when it is set to null', () => {
let testAppComponent = fixture.debugElement.componentInstance;
testAppComponent.selectedPortal = new ComponentPortal(PizzaMsg);

fixture.detectChanges();
expect(testAppComponent.portalHost.hasAttached()).toBe(true);
expect(testAppComponent.portalHost.portal).toBe(testAppComponent.selectedPortal);

testAppComponent.selectedPortal = null;
fixture.detectChanges();

expect(testAppComponent.portalHost.hasAttached()).toBe(false);
expect(testAppComponent.portalHost.portal).toBeNull();
});

it('should set the `portal` when attaching a component portal programmatically', () => {
let testAppComponent = fixture.debugElement.componentInstance;
let portal = new ComponentPortal(PizzaMsg);

testAppComponent.portalHost.attachComponentPortal(portal);

expect(testAppComponent.portalHost.portal).toBe(portal);
});

it('should set the `portal` when attaching a template portal programmatically', () => {
let testAppComponent = fixture.debugElement.componentInstance;
fixture.detectChanges();

testAppComponent.portalHost.attachTemplatePortal(testAppComponent.cakePortal);

expect(testAppComponent.portalHost.portal).toBe(testAppComponent.cakePortal);
});

it('should clear the portal reference on destroy', () => {
let testAppComponent = fixture.debugElement.componentInstance;

testAppComponent.selectedPortal = new ComponentPortal(PizzaMsg);
fixture.detectChanges();

expect(testAppComponent.portalHost.portal).toBeTruthy();

fixture.destroy();

expect(testAppComponent.portalHost.portal).toBeNull();
});
});

describe('DomPortalHost', () => {
Expand Down Expand Up @@ -342,6 +389,7 @@ class ArbitraryViewContainerRefComponent {
})
class PortalTestApp {
@ViewChildren(TemplatePortalDirective) portals: QueryList<TemplatePortalDirective>;
@ViewChild(PortalHostDirective) portalHost: PortalHostDirective;
selectedPortal: Portal<any>;
fruit: string = 'Banana';

Expand Down