別のコンポーネントのコード内からAngular2のコンポーネントをインスタンス化する方法を探しています。同様の質問をした多くの人々とは違って、私は新しいコンポーネントを動的にコンパイルすることにそれほど関心を持っていません。アプリケーションに既に存在するインスタンスをインスタンス化して挿入するだけです。例えばAngular 2.0 final:コードからコンポーネントをインスタンス化する方法
:
ダッシュボード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() {}
}
:
は、私は2つの要素を持っていると言います
私が探しているのは、DashboardComponentのonInitにDashboardItemComponentを作成し、それを#placeholder divに追加する方法です。注意すべき
2つのこと:
- 私は明示的にこれらのようにその場で新しいコンポーネントをコンパイルする話ではない子コンポーネント
- の入力/出力を使用することができるようにする必要があります二つの問題が説明します。How can I use/create dynamic template to compile dynamic Component with Angular 2.0?とEquivalent of $compile in Angular 2
をこれら二つの以前の問題は、同様の質問をするが、その答えはどちらかという精彩を欠いていないか、Angular2の以前の(ベータ)版に関連して、もはや機能しているようです。 How to instantiate and render Angular2 components?
あなたがのためにhttp://stackoverflow.com/questions/39678963/load-existing-components-dynamically-angular-2-final-release/39680765#39680765 – yurzui