I'm looking for a way to instantiate a component in Angular2 from within the code of another component. Unlike the many people that have asked a similar question I'm not so much interested in dynamically compiling a new component, just instantiating and inserting one that already exists in my application.
For instance:
Say I have two components:
dashboard-item.component.ts
import { Component } from "@angular/core";
@Component({
selector: "dashboard-item",
template: "Some dashboard item with functionality"
})
export class DashboardItemComponent {
constructor() {}
onInit() {}
}
dashboard.component.ts
import { Component } from "@angular/core";
@Component({
selector: "dashboard",
template: "<h1>Dashboard!</h1><div #placeholder></div>"
})
export class DashboardComponent {
constructor() {}
onInit() {}
}
What I'm looking for is a way to create a DashboardItemComponent in the onInit of the DashboardComponent and add it to the #placeholder div.
Two things to note:
- I will need to be able to use the inputs/outputs of the child component
- I'm explictly not talking about compiling a new component on the fly like these two issues describe: How can I use/create dynamic template to compile dynamic Component with Angular 2.0? and Equivalent of $compile in Angular 2
These two earlier issues ask a similar question, but their answers are either rather lackluster or pertain to earlier (beta) versions of Angular2 and no longer seem to work.