diff --git a/packages/core/src/render3/instructions.ts b/packages/core/src/render3/instructions.ts index 593577b095..ded6568583 100644 --- a/packages/core/src/render3/instructions.ts +++ b/packages/core/src/render3/instructions.ts @@ -1563,6 +1563,9 @@ export function embeddedViewEnd(): void { const lContainer = containerNode.data; if (creationMode) { + // When projected nodes are going to be inserted, the renderParent of the dynamic container + // used by the ViewContainerRef must be set. + setRenderParentInProjectedNodes(lContainer.renderParent, viewNode); // it is a new view, insert it into collection of views for a given container insertView(containerNode, viewNode, lContainer.nextIndex); } @@ -1574,6 +1577,32 @@ export function embeddedViewEnd(): void { ngDevMode && assertNodeType(previousOrParentNode, LNodeType.View); } +/** + * For nodes which are projected inside an embedded view, this function sets the renderParent + * of their dynamic LContainerNode. + * @param renderParent the renderParent of the LContainer which contains the embedded view. + * @param viewNode the embedded view. + */ +function setRenderParentInProjectedNodes( + renderParent: LElementNode | null, viewNode: LViewNode): void { + if (renderParent != null) { + let node = viewNode.child; + while (node) { + if (node.type === LNodeType.Projection) { + let nodeToProject: LNode|null = (node as LProjectionNode).data.head; + const lastNodeToProject = (node as LProjectionNode).data.tail; + while (nodeToProject) { + if (nodeToProject.dynamicLContainerNode) { + nodeToProject.dynamicLContainerNode.data.renderParent = renderParent; + } + nodeToProject = nodeToProject === lastNodeToProject ? null : nodeToProject.pNextOrParent; + } + } + node = node.next; + } + } +} + ///////////// /** diff --git a/packages/core/test/render3/view_container_ref_spec.ts b/packages/core/test/render3/view_container_ref_spec.ts index 4b49204b57..a79c7ba365 100644 --- a/packages/core/test/render3/view_container_ref_spec.ts +++ b/packages/core/test/render3/view_container_ref_spec.ts @@ -471,7 +471,7 @@ describe('ViewContainerRef', () => { textBinding(1, ctx.name); } - it('should project the ViewContainerRef content along its host', () => { + it('should project the ViewContainerRef content along its host, in an element', () => { @Component({selector: 'child', template: '