如何解决如何从ViewContainerRef-Angular 9获取子组件?
我在viewContainerRef内部添加了动态组件(ChildComponent.ts
)。我可以像这样从viewContainerRef.length
从viewContainerRef获取子计数,如果我使用索引来获取子组分(viewContainerRef.get(0)
),则返回ViewRef
,但是我需要子组分该怎么做。
示例:
@ViewChild("cards_holder",{ read: ViewContainerRef }) cards_holder: ViewContainerRef;
const componentFactory = this.resolver.resolveComponentFactory( SimpleCardComponent);
const componentRef = this.cards_holder.createComponent(componentFactory);
for (let index = 0; index < this.cards_holder.length; index++) {
console.log(this.cards_holder.get(index)) //It's Return ViewRef but i need SimpleCardComponent
}
谢谢。 Sabish.M
解决方法
自createComponent返回组件引用以来,您可以像这样访问SimpleCardComponent实例:
money.instance.setMoney(-turretBuildCost);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。