fix(ivy): properly insert views before ng-container with injected ViewContainerRef (#33853)

PR Close #33853
This commit is contained in:
Pawel Kozlowski 2019-11-15 12:18:00 +01:00 committed by Alex Rickabaugh
parent e698d355c1
commit 6bf2531b19
2 changed files with 38 additions and 2 deletions

View File

@ -682,7 +682,6 @@ function getFirstNativeNode(lView: LView, tNode: TNode | null): RNode|null {
TNodeType.IcuContainer, TNodeType.Projection); TNodeType.IcuContainer, TNodeType.Projection);
const tNodeType = tNode.type; const tNodeType = tNode.type;
if (tNodeType === TNodeType.Element) { if (tNodeType === TNodeType.Element) {
return getNativeByTNode(tNode, lView); return getNativeByTNode(tNode, lView);
} else if (tNodeType === TNodeType.Container) { } else if (tNodeType === TNodeType.Container) {
@ -692,7 +691,12 @@ function getFirstNativeNode(lView: LView, tNode: TNode | null): RNode|null {
if (elIcuContainerChild !== null) { if (elIcuContainerChild !== null) {
return getFirstNativeNode(lView, elIcuContainerChild); return getFirstNativeNode(lView, elIcuContainerChild);
} else { } else {
return getNativeByTNode(tNode, lView); const rNodeOrLContainer = lView[tNode.index];
if (isLContainer(rNodeOrLContainer)) {
return getBeforeNodeForView(-1, rNodeOrLContainer);
} else {
return unwrapRNode(rNodeOrLContainer);
}
} }
} else { } else {
const componentView = lView[DECLARATION_COMPONENT_VIEW]; const componentView = lView[DECLARATION_COMPONENT_VIEW];

View File

@ -578,5 +578,37 @@ describe('view insertion', () => {
expect(fixture.nativeElement.textContent).toBe('container start|test|container end|click'); expect(fixture.nativeElement.textContent).toBe('container start|test|container end|click');
}); });
it('should properly insert before views in a ViewContainerRef injected on ng-container', () => {
@Component({
selector: 'app-root',
template: `
<ng-template #parameterListItem let-parameter="parameter">
{{parameter}}
</ng-template>
<ng-container *ngFor="let parameter of items;"
[ngTemplateOutlet]="parameterListItem"
[ngTemplateOutletContext]="{parameter:parameter}">
</ng-container>
`
})
class AppComponent {
items = [1];
}
TestBed.configureTestingModule({
declarations: [AppComponent],
imports: [CommonModule],
});
const fixture = TestBed.createComponent(AppComponent);
fixture.detectChanges();
expect(fixture.nativeElement.textContent.trim()).toContain('1');
fixture.componentInstance.items = [2, 1];
fixture.detectChanges();
expect(fixture.nativeElement.textContent.trim()).toContain('2 1');
});
}); });
}); });