angular - Angular 9:创建动态 ViewContainerRef 以循环组件数组
问题描述
我正在尝试使用具有不同组件的 angular2gridster 创建仪表板卡网格页面作为每张卡的内容。卡片是在指定卡片数据的对象数组上使用 NgFor 创建的,包括包含组件的属性(如下所示)。不知何故,一个错误显示在 ViewContainerRef 是如何未定义的,尽管如果我将模板移出 NgFor 范围是很好的。
{
component: CashflowOverviewComponent,
title: 'example title',
visible: true,
metadata: {
//XY coordinate in grid
x: 0,
y: 0,
//Width and height
w: 2,
h: 1,
},
},
在我的 HTML 中,我尝试使用 NgFor 生成代码
<div *ngFor="let widget of widgets">
<ngx-gridster-item *ngIf="widget.visible" class="grid_card" [(x)]="widget['metadata'].x "
[(y)]="widget['metadata'].y" [(w)]="widget['metadata'].w" [(h)]="widget['metadata'].h">
<div class="card">
<div class="card_header">
<div *ngIf="dragEnabled" class="material-icons card_drag handler_icon drag_icon">drag_indicator
</div>
<div class="card_title">Card title</div>
</div>
<ng-template #cardContent></ng-template>
{{dynamicComponentGenerator(widget.component)}}
<div class="card_content">
</div>
</div>
</ngx-gridster-item>
</div>
下面的代码是我如何尝试初始化
@ViewChild("cardContent",{static:true , read: ViewContainerRef}) cardContent: ViewContainerRef;QueryList<ViewContainerRef>;
这是我用来尝试让组件显示的功能
dynamicComponentGenerator = (component)=>{
let factory = this.r.resolveComponentFactory(component);
this.cardContent.clear();
let compRef = this.cardContent.createComponent(factory);
}
解决方案
推荐阅读
- python - 将 graphviz Source 对象转换为 graphviz Graph 对象
- java - 有没有办法监听和检测安装和卸载?
- javascript - 在 ReactJS 中使用单独的方法填充渲染方法
- python - 如何在键值中存储大整数值:python中字典的值对?
- angular - Ng 更新不适用于错误“minTimeout 大于 maxTimeout”
- java - Apache Camel HTTP 超时
- matlab - MATLAB中的边界值问题,fsolve问题
- java - 从 Java 中的 Map 中删除元素
- office-js - Office 加载项图标不显示在桌面应用程序上
- c++ - 当 /usr/local 中存在普通 brew 包时,针对 Keg (/usr/local/opt) 构建/链接