I am attempting to dynamically inject a component into a container.
Component:
@Component({...})
export class InvestmentProcess{
@ViewChild('container') container;
constructor(public dcl: DynamicComponentLoader) {}
loadComponent(fooComponent: Type){
this.dcl.loadNextToLocation(fooComponent, container);
}
}
Template:
<div #container> </div>
Upon running the loadComponent
function, an error is thrown:
TypeError: location.createComponent is not a function
This is because the variable container
is of type ElementRef
, while loadNextToLocation
expects a ViewContainerRef
as the second parameter. According to the official documentation, the ViewContainerRef
can be obtained from an element using @ViewChild
, but I have not found a proper example to do so.
Working with Angular2.0.0rc1