コンポーネントを使用しようとしています。このコンポーネントのテンプレートは、その中の同じコンポーネントの別のインスタンスを参照しています。それは次のようになりJSONとして表現コンポーネントを再帰的に使用する
myBox = new Box('top box');
myBox.contents.add(new Item('level 2 - 1'));
myBox.contents.add(new Item('level 2 - 2'));
Box myBox2 = new Box('inner box');
myBox2.contents.add(new Item('level 3 - 1'));
myBox2.contents.add(new Item('level 3 - 2'));
myBox.contents.add(myBox2);
:
をclass Item {
String name;
Item(this.name);
}
class Box extends Item {
Box(String name) : super(name);
List<Item> contents = [];
}
上記に基づき、データは、このように作成されます。
モデルは、このようなものです
{ "name": "top box", "contents": [ {"name": "level 2 - 1"}, {"name": "level 2 - 2"}, {"name": "inner box", "contents": [ {"name": "level 3 - 1"}, {"name": "level 3 - 2"} ] } ] }
成分ダーツ(box_component.dart):
@Component(
selector: 'box-component',
templateUrl: 'box_component.html',
directives: const[CORE_DIRECTIVES]
)
class BoxComponent implements OnInit {
@Input()
Box box;
List contents =[];
ngOnInit() {
contents = box.contents;
}
isBox(Item itm) => (itm is Box);
}
コンポーネントテンプレート(box_component.html):ボックスコンポーネントの別のインスタンスが内にネストされていることを
<div *ngIf="box != null" style="font-weight: bold">{{box.name}}</div>
<div *ngFor="let item of contents">
{{item.name}}
<div *ngIf="isBox(item)">
This is a box
<box-component [box]="item"></box-component>
</div>
<div *ngIf="!isBox(item)">
This is an item
</div>
</div>
注トップレベルのボックスコンポーネント しかし、レンダリング時には、トップレベルボックスの内容のみがレンダリングされ、ボックス内に入れ子になっているボックスには含まれません。
それは次のようになります
トップボックス
レベル2 - これはアイテム
内箱
これは、ある2
- 1
これはアイテム
レベル2でありますボックス
これは商品です
再帰を達成する方法はありますかアイディアネストされたレンダリング?
でなければなりません。あなたはそれに間違いがあります。 '* ngFor 'は空の配列に対して何も描画しないので、' * ngIf'は冗長です。それに加えて、ネストされたコンポーネントには特別なものはありません。 –
あなたはそうです、@GünterZöchbauer。それはいくつかのエラーを投げていた、私はそれらを無視していた。指摘してくれてありがとう、私は私の質問の詳細を修正しています。しかし、私はまだネストされたコンポーネントの内容を取得していません。 – Hesh
上記のディレクティブリストは正しいですか?あなたは、BoxComponentが正しくないCORE_DIRECTIVESを使用していると言うだけで、BoxComponentも使用しようとしています。 –