首页 > 解决方案 > 无法在 Angular 中编辑动态组件

问题描述

我正在以通常的方式创建一个动态组件,如下所示。

const formRowFactory = this.resolver.resolveComponentFactory(FormRowComponent);
const { instance: formRowComponent } = this.newComponents.createComponent(formRowFactory);
formRowComponent.name = categoryName;

我将它的索引存储在别处,以便检索组件

const component = this.newComponents.get(index);

然后我想编辑名称,但我做不到。它不允许我访问该实例,或者没有更改它的功能。

在此处输入图像描述

你是怎么做到的?顺便说一句,在 Angular 中编辑内容子项非常容易。为什么对动态的如此苛刻?

标签: angularangular-dynamic-components

解决方案


我相信这是因为当您写信时,this.newComponents.get(index)您会收到ViewRef而不是ComponentRef。我使用动态创建的组件引用维护一个单独的数组,以便以后访问它们。

角度文档


推荐阅读