From 8b2dfb2eca83b571f7c87db20297169eb5657773 Mon Sep 17 00:00:00 2001 From: Tobias Bosch Date: Wed, 16 Nov 2016 10:00:18 -0800 Subject: [PATCH] fix(core): support `ngTemplateOutlet` in production mode (#12921) Fixes #12911 --- modules/@angular/core/src/linker/view.ts | 6 ++- .../direct_rendering_integration_spec.ts | 42 ++++++++++++++++++- 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/modules/@angular/core/src/linker/view.ts b/modules/@angular/core/src/linker/view.ts index 5c8bb69ddb..5c76cba583 100644 --- a/modules/@angular/core/src/linker/view.ts +++ b/modules/@angular/core/src/linker/view.ts @@ -250,8 +250,10 @@ export abstract class AppView { if (nextSibling) { this.visitRootNodesInternal(this._directRenderer.insertBefore, nextSibling); } else { - this.visitRootNodesInternal( - this._directRenderer.appendChild, this._directRenderer.parentElement(prevNode)); + const parentElement = this._directRenderer.parentElement(prevNode); + if (parentElement) { + this.visitRootNodesInternal(this._directRenderer.appendChild, parentElement); + } } } else { this.renderer.attachViewAfter(prevNode, this.flatRootNodes); diff --git a/modules/@angular/core/test/linker/direct_rendering_integration_spec.ts b/modules/@angular/core/test/linker/direct_rendering_integration_spec.ts index 73d68ae6a3..983098feed 100644 --- a/modules/@angular/core/test/linker/direct_rendering_integration_spec.ts +++ b/modules/@angular/core/test/linker/direct_rendering_integration_spec.ts @@ -6,7 +6,7 @@ * found in the LICENSE file at https://angular.io/license */ -import {Component, Injectable, RenderComponentType, Renderer, RootRenderer} from '@angular/core'; +import {Component, ContentChild, Injectable, Input, RenderComponentType, Renderer, RootRenderer, TemplateRef} from '@angular/core'; import {DebugDomRenderer} from '@angular/core/src/debug/debug_renderer'; import {DirectRenderer} from '@angular/core/src/render/api'; import {TestBed, inject} from '@angular/core/testing'; @@ -125,6 +125,46 @@ export function main() { const projectedNode = childHostEl.childNodes[1]; expect(directRenderer.appendChild).toHaveBeenCalledWith(projectedNode, childHostEl); }); + + it('should support using structural directives with ngTemplateOutlet', () => { + @Component({ + template: + '' + }) + class Parent { + templateCtx = {shown: false}; + } + + @Component({ + selector: 'child', + template: + '()' + }) + class Child { + @Input() + templateCtx: any; + + @ContentChild('tpl') + templateRef: TemplateRef; + } + + TestBed.configureTestingModule({declarations: [Parent, Child]}); + + let fixture = TestBed.createComponent(Parent); + fixture.componentInstance.templateCtx.shown = false; + fixture.detectChanges(); + expect(fixture.nativeElement).toHaveText('()'); + fixture.destroy(); + + fixture = TestBed.createComponent(Parent); + fixture.componentInstance.templateCtx.shown = true; + fixture.detectChanges(); + expect(fixture.nativeElement).toHaveText('(hello)'); + + fixture.componentInstance.templateCtx.shown = false; + fixture.detectChanges(); + expect(fixture.nativeElement).toHaveText('()'); + }); }); }