2017-02-03 3 views
0

コンポーネントのselectorタグの中に書かれたhtmlコードをそのまま(ブラウザにレンダリングする前に)取得する必要があります。たとえば、私は、文字列としてタグ内に記述されたHTMLコードを必要とする子コンポーネントでは、私のコンポーネントは、親と子ですAngular2でレンダリングする前にng-contentを取得する方法

子で

、親で

@Component({ 
    selector: 'child', 
    template: '<ng-content></ng-content>' 
}) 
... 

@Component({ 
    selector: 'parent', 
    template: ` 
     <child> 
      <p>Title 1</p> 
      <some-other-component [input]="1"></some-other-component> 
     </child> 


     <child> 
      <p>Title 2</p> 
      <some-other-component [input]="2"></some-other-component> 
     </child> 
    ` 
}) 
... 

を前提としています。上記のシナリオでは、"<p>Title 1</p> <some-other-component [input]="1"></some-other-component>""<p>Title 2</p> <some-other-component [input]="2"></some-other-component>"が必要です。

P.S.ここでの目的は、コードスニペットページを作成することです.HTMLコードスニペットをng-contentとして渡してプレビューとコードサンプルの両方をレンダリングすることができます。

答えて

0

コンテンツパートに一意のクラスを追加し、次のコンポーネント設定を使用して選択することができます。ここでのセレクタは、任意の有効なquerySelector FNセレクタは、だからあなたのコンテンツがこの

<child class="fisrt"> 
     <p>Title 1</p> 
     <some-other-component [input]="1"></some-other-component> 
    </child> 


    <child class="second"> 
     <p>Title 2</p> 
     <some-other-component [input]="2"></some-other-component> 
    </child> 
ようにすることができ

<ng-content select=".first"> 

<ng-content select=".second"> 

です

関連する問題