2016-04-24 3 views
2

は一例です:コンポーネントタグまたはhtmlタグではないコンポーネントタグの内部テキストにアクセスしますか?ここ

var ListItem = ng.core.Component({ 
    selector: "item", 
    inputs: ["title"], 
    template: "<li>{{title}} | <ng-content></ng-content></li>", 
}).Class({ 
    constructor: function(){} 
}) 

var ListOne = ng.core.Component({ 
    selector: "listone", 
    queries: { 
     list_items: new ng.core.ContentChildren(ListItem) 
    }, 
    directives: [ListItem], 
    template: "<ul><ng-content select='item'></ng-content></ul>" 
}).Class({ 
    constructor: function(){}, 
    ngAfterContentInit: function(){ 
     console.log(this.list_items); 
    } 
}) 

そして私は、コンポーネント表示するには、このコードを使用しています:<item>タグのListOneコンポーネントアクセスinnerHTMLプロパティの

<listone> 
    <item title="first">first</item> 
    <item title="second">second</item> 
</listone> 

どのようngAfterContentInitを?

別のタグで内側のテキストを折り返さずに解決策が必要です。

export class ListoneComponent { 
    @ContentChildren(ItemComponent, {descendants: true}) list_items; 

    ngAfterContentInit() { 
    this.list_items.toArray().forEach((item) => { 
     console.log(item.elementRef.nativeElement.innerHTML); 
    }) 
    } 
} 

Plunker example

は、私は、活字体のコードは依然として有用であると思います。私たちはdescendants: trueを設定@ContentChildren()するため

export class ItemComponent { 
    title: 'title'; 
    constructor(public elementRef:ElementRef){} 
} 

答えて

2

これは、ビットItemComponentのサポートを必要としますAngular with ESxの使い方を知らない

+0

この例は、次のようになります。https://plnkr.co/edit/EdSONOCq9bfadD42jOj2?p=preview申し訳ありませんが、なぜ子孫プロパティを持つオブジェクトを渡すのですか? – yurzui

+0

それ以外の場合は直接の子のみが返されますが、他の子孫は返されないためです。項目はプロジェクトの2つのレベルの深い子供だけで動作しませんので、 ES5 Plunkerありがとう! –

関連する問題