fix(core): support `ngTemplateOutlet` in production mode (#12921)

Fixes #12911
This commit is contained in:
Tobias Bosch 2016-11-16 10:00:18 -08:00 committed by Victor Berchet
parent 824ea8406c
commit 8b2dfb2eca
2 changed files with 45 additions and 3 deletions

View File

@ -250,8 +250,10 @@ export abstract class AppView<T> {
if (nextSibling) { if (nextSibling) {
this.visitRootNodesInternal(this._directRenderer.insertBefore, nextSibling); this.visitRootNodesInternal(this._directRenderer.insertBefore, nextSibling);
} else { } else {
this.visitRootNodesInternal( const parentElement = this._directRenderer.parentElement(prevNode);
this._directRenderer.appendChild, this._directRenderer.parentElement(prevNode)); if (parentElement) {
this.visitRootNodesInternal(this._directRenderer.appendChild, parentElement);
}
} }
} else { } else {
this.renderer.attachViewAfter(prevNode, this.flatRootNodes); this.renderer.attachViewAfter(prevNode, this.flatRootNodes);

View File

@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license * 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 {DebugDomRenderer} from '@angular/core/src/debug/debug_renderer';
import {DirectRenderer} from '@angular/core/src/render/api'; import {DirectRenderer} from '@angular/core/src/render/api';
import {TestBed, inject} from '@angular/core/testing'; import {TestBed, inject} from '@angular/core/testing';
@ -125,6 +125,46 @@ export function main() {
const projectedNode = childHostEl.childNodes[1]; const projectedNode = childHostEl.childNodes[1];
expect(directRenderer.appendChild).toHaveBeenCalledWith(projectedNode, childHostEl); expect(directRenderer.appendChild).toHaveBeenCalledWith(projectedNode, childHostEl);
}); });
it('should support using structural directives with ngTemplateOutlet', () => {
@Component({
template:
'<child [templateCtx]="templateCtx"><template let-shown="shown" #tpl><span *ngIf="shown">hello</span></template></child>'
})
class Parent {
templateCtx = {shown: false};
}
@Component({
selector: 'child',
template:
'(<template [ngTemplateOutlet]="templateRef" [ngOutletContext]="templateCtx"></template>)'
})
class Child {
@Input()
templateCtx: any;
@ContentChild('tpl')
templateRef: TemplateRef<any>;
}
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('()');
});
}); });
} }