'use strict'; import { Injectable, ComponentFactory, ComponentRef, ViewContainerRef } from '@angular/core'; @Injectable() export class ContentProjector { instantiateAndProject(componentFactory: ComponentFactory, parentView:ViewContainerRef, projectedNodesOrComponents: any[]):ComponentRef { let contextInjector = parentView.parentInjector; let projectedNodes = []; let componentRefs:ComponentRef[] = []; for (let i=0; i < projectedNodesOrComponents.length; i++) { let nodeOrCompRef = projectedNodesOrComponents[i]; if (nodeOrCompRef instanceof ComponentRef) { projectedNodes.push(nodeOrCompRef.location.nativeElement); componentRefs.push(nodeOrCompRef); } else { projectedNodes.push(nodeOrCompRef); } } let parentCompRef = parentView.createComponent(componentFactory, null, contextInjector, [projectedNodes]); // using private property to get view instance let viewContainer = (parentView)._view; let viewData = (parentView)._data; viewData.embeddedViews = viewData.embeddedViews || []; for (let i=0; i < componentRefs.length; i++) { let compRef = componentRefs[i]; // attach view to containter change detector viewData.embeddedViews.push((compRef.hostView)._view); (compRef.hostView).attachToViewContainerRef(viewContainer); } return parentCompRef; } }