[英]Angular Nested Dynamically Components
我想從給定的 json 動態創建組件。 第一層是TestComponent,可以有很多TestLayerComponent。 問題是TestLayerComponent 也可以有很多TestLayerComponent。 我用它來動態制作幻燈片推送菜單。 如果圖層路徑以歐洲開頭,則使用文本歐洲創建<li>
,如果歐洲有巴爾干,則在歐洲創建新的<ul>
,使用<li>
巴爾干等。
菜單必須看起來像這樣,但使用不使用 DOM 的組件實現。
[{
Username:"",
Password:"",
Maps:[
{
Name:"",
Title:"",
Layers:[
id:""
path:"Europe/Balkans/Greece"
]
},
{
Name:"",
Title:"",
Layers:[
id:""
path:"Europe/Balkans/Romania"
]
},
{
Name:"",
Title:"",
Layers:[
id:""
path:"Asia/China"
]
},
]
}
]
這是我嘗試但仍僅顯示最后一個組件的代碼。
for (const map of maps) {
mapViewContainerRef.clear();
let mapRow = mapViewContainerRef.createComponent(mapFactory);
mapRow.instance.maps = map;
for (const layer of map.layers) {
mapFactory = this.menuFactory.resolveComponentFactory(TestLayerComponent);
mapViewContainerRef.clear();
mapViewContainerRef.createComponent(mapFactory);
mapRow = mapViewContainerRef.createComponent(mapFactory);
mapRow.instance.layers = layer;
}
mapRow.destroy();
}
for (const map of maps) {
mapViewContainerRef.clear();
let mapRow = mapViewContainerRef.createComponent(mapFactory);
let index = 1;
mapRow.instance.maps = map;
for (const layer of map.layers) {
mapFactory = this.menuFactory.resolveComponentFactory(TestLayerComponent);
mapRow = mapViewContainerRef.createComponent(mapFactory, index);
mapRow.instance.layers = layer;
index++;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.