2016-07-29 18 views
0

私は基本的にコンポーネントを入れ子にしようとしています。私が持っているコードで説明するのが簡単になると思います。データバインディングを持つコンポーネントでディレクティブを使用するにはどうすればよいですか?

基本的に私は2つのコンポーネントを持っています、私は特定の方法でもう一方のコンポーネントを使用したいです。ここで私は、彼らが設定した方法は次のとおりです。


test.component.ts

import { Component } from '@angular/core'; 

@Component({ 
    selector: 'test', 
    templateUrl: 'partials/test.html' 
}) 
export class TestComponent { 
    constructor() {} 
} 

another.component.ts

import { Component } from '@angular/core'; 
import { TestComponent } from './test.component'; 

interface Circle { 
    show: boolean, 
    data: any 
} 

@Component({ 
    selector: 'another', 
    directives: [ TestComponent ], 
    templateUrl: '/partials/another.html' 
}) 
export class AnotherComponent { 
    circles: Circle[] = []; 

    constructor() { 
     let testElement = document.createElement('test'), 
      defaultCircles = [ 
       { show: true, data: testElement } 
      ]; 

     this.circles = defaultCircles; 
    } 
} 

partials/another.htmlは:

<ul> 
    <li *ngFor="let circle of circles; let i = index"> 
     {{circle.data}} <!-- this just shows [object HTMLElement] --> 
    </li> 
</ul> 

ディレクティブの内容を表示するにはどうすればよいですか?私はdata: testElementdata: '<test></test>'に置き換えようとしましたが、それでも動作しません、それはちょうどテキスト<test></test>を示しています。

+0

「circle.data」の内容は何ですか? –

+0

'data:testElement'とtestElement私は' document.createElement( 'test') 'と' ' – Jared

答えて

1

これはうまくいくかもしれない:

<ul> 
    <li *ngFor="let circle of circles; let i = index" [innerHTML]="circle.data"> 
    </li> 
</ul> 
+0

の両方でテストしています。これをページのHTML要素としてレンダリングしますが、 partials/test.html'はそこにはありません。 – Jared

+1

スニペットから何が起こっているのか分かりません。動的に追加されたHTMLは、コンポーネントやディレクティブをインスタンス化したり、バインディングを解決したりしません。 Angular2は、HTMLをブラウザに渡す以外は無視します。 http://stackoverflow.com/questions/36325212/angular-2-dynamic-tabs-with-user-click-chosen-components/36325468#36325468のように、 'ViewContainerRef.createComponent()'のようなものが必要です –

関連する問題