angular - 使用构造函数参数创建动态组件
问题描述
有没有办法动态创建需要构造函数参数的组件?
澄清一下:我使用 ComponentFactory 创建组件,如下所示:
const factory = this.resolver.resolveComponentFactory(MyComponent);
const componentRef: ComponentRef<MyComponent> = this.myInsertionpoint.createComponent(factory);
该组件有一个只读字段,应该通过构造函数设置。
export class MyComponent {
private readonly _myField: MyField;
constructor(myField: MyField) {
this._myField = myField;
}
...
}
我知道我可以删除只读设置字段,例如
component.instance.myField = "myValue";
...但我宁愿保持原样,因为该字段仅应在创建组件时设置。
解决方案
推荐阅读
- android - 避免从 WorkManager 复制 PeriodicWorkRequest
- node.js - 在 nodejs 的调试模块中用作参数时,冒号是什么意思?
- ssl - 共享主机上子域的 SSL
- groovy - Groovy 范围在两个字符后停止工作?
- for-loop - 从流中的其他流返回值
- javascript - ASP 多个回发重叠
- javascript - 为什么 document.getElementById() 在角度 4 中递归调用函数后返回 null?
- r - 闪亮:updateSelectInput 之后的 if 语句
- php - Laravel updateTimestamps 方法不必要的检查?
- php - laravel 5.4 中的子查询