gpt4 book ai didi

angular - 使用参数动态创建组件

转载 作者:太空狗 更新时间:2023-10-29 18:18:44 26 4
gpt4 key购买 nike

我有一个使用 ComponentFactoryResolver 动态创建的组件

this.container.clear();
let factory = this.resolver.resolveComponentFactory(DynamicComponent);
this.componentRef = this.container.createComponent(factory);

template: '...<child-component [param1]="param1"></child-component>...';

问题是 DynamicComponent 的模板有子组件和输入绑定(bind)。有没有办法在动态创建组件时给子组件传递参数?

最佳答案

是这样的:

const factory = this.componentFactoryResolver.resolveComponentFactory(LoginComponent);
const component: ComponentRef<LoginComponent> = this.viewContainerRef.createComponent(factory);
component.instance.user = "prop 1";
component.instance.input2 = "prop 2";

关于angular - 使用参数动态创建组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50429163/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com