Load existing components dynamically Angular 2 Final Releaseのような多くのstackoverflowオプションを試しました。Angular2のコンポーネントでダイナミックテンプレートをレンダリングする方法
私がしたいことは、ajaxリクエストを持つhtmlページを取得し、カスタムコンポーネントでこのテンプレートをレンダリング/コンパイルすることです。
私は、angular2に2つの非推奨コンポーネントがあり、使用する必要があることを理解しましたComponentFactoryResolver。
私の古いソリューションでは、HTMLをレンダリングするために '[innerHtml]'を設定することができます。 今、私は新しい解決策が必要です。
誰が私を助けることができますか?
page.component.ts
import { Component, ViewChild, ViewContainerRef, ComponentFactory, OnInit, ComponentFactoryResolver } from '@angular/core';
import { ActivatedRoute, Params } from '@angular/router';
@Component({
selector: "wd-page",
templateUrl: "/app/page/page.component.html",
providers: []
})
export class PageComponent implements OnInit {
// we need the viewcontainer ref, so explicitly define that, or we'll get back
// an element ref.
@ViewChild('dynamicChild', { read: ViewContainerRef })
private target: ViewContainerRef;
private page = {
Source: "<div><h2>Hello world</h2><one-of-my-components></one-of-my-components></div>"
}
constructor(
private vcRef: ViewContainerRef,
private resolver: ComponentFactoryResolver) { }
ngOnInit() {
//What code do i need here?
}
}
<div #dynamicChild></div>
<!-- Old implementation!
<div *ngIf="!showSource" [innerHTML]="page">
</div>
-->
で私たち自身のコンポーネントをエクスポートします。 http://stackoverflow.com/questions/40060498/angular-2-1-0-create-child-component-on-the-fly-dynamically/40080290#40080290あなたが望むことをする可能性があります。あなたは[ComponentFactoryResolver](https://angular.io/docs/ts/latest/api/core/index/ComponentFactoryResolver-class.html)は推奨されていませんと思いますか? –
こんにちはガンター、私はこのソリューションを試してみましたが、それは本当の角度のコンポーネントではなく、カスタムコンポーネントでのみ動作します。私はあなたから私の問題を再現するためのポストを編集しました。 https://plnkr.co/edit/UACDPBRWNmvjVVsr0dWC – Linksonder
これはカスタムコンポーネントで動作しますhttps://plnkr.co/edit/TAbupH4si62x10QZ7xuc?p=preview – yurzui