angular - Angular 嵌套动态组件
问题描述
我想从给定的 json 动态创建组件。第一层是TestComponent,可以有很多TestLayerComponent。问题是TestLayerComponent 也可以有很多TestLayerComponent。我用它来动态制作幻灯片推送菜单。如果图层路径以 Europe 开头<li>
,则使用文本 Europe 创建,如果 Europe 有 Balkans,则使用 Balkans<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++;
}
}
推荐阅读
- typescript - 对象的类型定义,它对类型的值进行(自我)引用
- sql - 两个时间戳和分类到 1 分钟存储桶之间的 SQL 差异
- jupyter - 瞧仪表板基本网址
- vb.net - 居中标签时,它位于面板后面
- python - 芹菜的原始 PyMySQL 多线程
- python - 为推销员做一个 Python 项目。出现“无法分配给操作员”错误
- shell - 当输入来自重定向时,为什么 bufio.NewReader 不能正常工作?
- python - 通过 python 发送电子邮件中的图像在 iOS 上不显示
- php - 关于从我在 drupal 中的自定义注册表单发送的邮件
- tooltip - ag-grid 中的 ngbtoolTip 不会关闭 onscroll