2016-10-17 5 views
10

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> 
--> 
+0

で私たち自身のコンポーネントをエクスポートします。 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)は推奨されていませんと思いますか? –

+0

こんにちはガンター、私はこのソリューションを試してみましたが、それは本当の角度のコンポーネントではなく、カスタムコンポーネントでのみ動作します。私はあなたから私の問題を再現するためのポストを編集しました。 https://plnkr.co/edit/UACDPBRWNmvjVVsr0dWC – Linksonder

+1

これはカスタムコンポーネントで動作しますhttps://plnkr.co/edit/TAbupH4si62x10QZ7xuc?p=preview – yurzui

答えて

8

問題はYurzuiにおかげで、

https://plnkr.co/edit/TAbupH4si62x10QZ7xuc?p=preview

異なるポスト(Angular 2.1.0 create child component on the fly, dynamically)から、一般的なHTMLのoutleteを解決することができますカスタムディレクティブでテンプレートをレンダリングするために使用するそれらにctives。

モジュールをレンダリングするすべてのカスタムコンポーネントとともにインポートすることを忘れないでください!

export function createComponentFactory(compiler: Compiler, metadata: Component): Promise<ComponentFactory<any>> { 
const cmpClass = class DynamicComponent {}; 
const decoratedCmp = Component(metadata)(cmpClass); 

// Import the module with required components here 
@NgModule({ imports: [CommonModule, RouterModule, SharedModule], declarations: [decoratedCmp] }) 
class DynamicHtmlModule { } 

return compiler.compileModuleAndAllComponentsAsync(DynamicHtmlModule) 
    .then((moduleWithComponentFactory: ModuleWithComponentFactories<any>) => { 
    return moduleWithComponentFactory.componentFactories.find(x => x.componentType === decoratedCmp); 
    }); 
} 
1

私は@Yurzuiと@Linksonderのソリューションで(例えばHomeComponentなど)私自身のコンポーネントを使用するための小さな変更を加えました。 https://plnkr.co/edit/27x0eg?p=preview

基本的には、createComponentFactory()内で追加のインポートとしてAppModuleをDynamicHtmlModuleに追加しています。

@NgModule({ imports: [AppModule, CommonModule, RouterModule, SharedModule], declarations: [decoratedCmp] }) 
class DynamicHtmlModule { } 

そしてAppModule

`[innerHTMLプロパティ]`コンポーネントを作成したことはありません
@NgModule({ 
    ... 
    exports: [HomeComponent, AboutComponent], 
    ... 
}) 
export class AppModule { } 
関連する問題