Skip to content
Closed
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
21 changes: 16 additions & 5 deletions packages/common/src/directives/ng_component_outlet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/

import {ComponentFactoryResolver, ComponentRef, Directive, Injector, Input, NgModuleFactory, NgModuleRef, OnChanges, OnDestroy, Provider, SimpleChanges, Type, ViewContainerRef} from '@angular/core';
import {ComponentFactoryResolver, ComponentRef, Directive, EventEmitter, Injector, Input, NgModuleFactory, NgModuleRef, OnChanges, OnDestroy, Output, Provider, SimpleChanges, Type, ViewContainerRef} from '@angular/core';

/**
* Instantiates a single {@link Component} type and inserts its Host View into current View.
Expand Down Expand Up @@ -73,14 +73,16 @@ export class NgComponentOutlet implements OnChanges, OnDestroy {
@Input() ngComponentOutletContent: any[][];
@Input() ngComponentOutletNgModuleFactory: NgModuleFactory<any>;

@Output('create') createEvents = new EventEmitter<any>();
@Output('destroy') destroyEvents = new EventEmitter<void>();

private _componentRef: ComponentRef<any> = null;
private _moduleRef: NgModuleRef<any> = null;

constructor(private _viewContainerRef: ViewContainerRef) {}

ngOnChanges(changes: SimpleChanges) {
this._viewContainerRef.clear();
this._componentRef = null;
ngOnChanges(changes: SimpleChanges): void {
this._destroyComponent();

if (this.ngComponentOutlet) {
const elInjector = this.ngComponentOutletInjector || this._viewContainerRef.parentInjector;
Expand All @@ -105,10 +107,19 @@ export class NgComponentOutlet implements OnChanges, OnDestroy {
this._componentRef = this._viewContainerRef.createComponent(
componentFactory, this._viewContainerRef.length, elInjector,
this.ngComponentOutletContent);
this.createEvents.emit(this._componentRef.instance);
}
}

ngOnDestroy() {
ngOnDestroy(): void {
if (this._moduleRef) this._moduleRef.destroy();
}

private _destroyComponent(): void {
if (this._componentRef) {
this._viewContainerRef.clear();
this._componentRef = null;
this.destroyEvents.emit();
}
}
}
37 changes: 33 additions & 4 deletions packages/common/test/directives/ng_component_outlet_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ export function main() {

it('should do nothing if component is null', async(() => {
const template = `<ng-template *ngComponentOutlet="currentComponent"></ng-template>`;
TestBed.overrideComponent(TestComponent, {set: {template: template}});
let fixture = TestBed.createComponent(TestComponent);
const fixture =
TestBed.overrideTemplate(TestComponent, template).createComponent(TestComponent);

fixture.componentInstance.currentComponent = null;
fixture.detectChanges();
Expand All @@ -29,15 +29,40 @@ export function main() {
}));

it('should insert content specified by a component', async(() => {
let fixture = TestBed.createComponent(TestComponent);
const fixture = TestBed.createComponent(TestComponent);

fixture.detectChanges();
expect(fixture.nativeElement).toHaveText('');

fixture.componentInstance.currentComponent = InjectedComponent;

fixture.detectChanges();
expect(fixture.nativeElement).toHaveText('foo');
}));

it(`should emit an event when a component gets created and destroyed`, async(() => {
const template =
'<ng-template [ngComponentOutlet]="currentComponent" (create)="onCreate($event)" (destroy)="onDestroy()"></ng-template>';
const fixture =
TestBed.overrideTemplate(TestComponent, template).createComponent(TestComponent);

fixture.detectChanges();
expect(fixture.nativeElement).toHaveText('');
expect(fixture.componentInstance.eventLog.length).toBe(0);

fixture.componentInstance.currentComponent = InjectedComponent;

fixture.detectChanges();
expect(fixture.nativeElement).toHaveText('foo');
expect(fixture.componentInstance.eventLog.length).toBe(1);
expect(fixture.componentInstance.eventLog[0]).toBeAnInstanceOf(InjectedComponent);

fixture.componentInstance.currentComponent = null;

fixture.detectChanges();
expect(fixture.nativeElement).toHaveText('');
expect(fixture.componentInstance.eventLog.length).toBe(2);
expect(fixture.componentInstance.eventLog[1]).toBe('destroyed');
}));

it('should emit a ComponentRef once a component was created', async(() => {
Expand Down Expand Up @@ -159,7 +184,6 @@ export function main() {
}));

it('should clean up moduleRef, if supplied', async(() => {
let destroyed = false;
const compiler = TestBed.get(Compiler) as Compiler;
const fixture = TestBed.createComponent(TestComponent);
fixture.componentInstance.module = compiler.compileModuleSync(TestModule2);
Expand Down Expand Up @@ -228,6 +252,7 @@ class TestComponent {
injector: Injector;
projectables: any[][];
module: NgModuleFactory<any>;
eventLog: any[] = [];

get cmpRef(): ComponentRef<any> { return this.ngComponentOutlet['_componentRef']; }
set cmpRef(value: ComponentRef<any>) { this.ngComponentOutlet['_componentRef'] = value; }
Expand All @@ -236,6 +261,10 @@ class TestComponent {
@ViewChild(NgComponentOutlet) ngComponentOutlet: NgComponentOutlet;

constructor(public vcRef: ViewContainerRef) {}

onCreate(cmp: any): void { this.eventLog.push(cmp); }

onDestroy(): void { this.eventLog.push('destroyed'); }
}

@NgModule({
Expand Down
2 changes: 2 additions & 0 deletions tools/public_api_guard/common/common.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ export declare class NgClass implements DoCheck {

/** @experimental */
export declare class NgComponentOutlet implements OnChanges, OnDestroy {
createEvents: EventEmitter<any>;
destroyEvents: EventEmitter<void>;
ngComponentOutlet: Type<any>;
ngComponentOutletContent: any[][];
ngComponentOutletInjector: Injector;
Expand Down