gpt4 book ai didi

Angular 2 : Load component dynamically with parameters @Input and @Output

转载 作者:太空狗 更新时间:2023-10-29 17:09:39 25 4
gpt4 key购买 nike

目前我正在使用这段代码动态加载我的一些组件。

export class ComponentOutlet {

constructor(
private vcRef: ViewContainerRef,
private compiler: Compiler,
private dataService: DataService
) { }

private _createDynamicComponent() {

// Some logic to decide which component should be loaded
return MyComponent;
}


ngOnChanges() {

this.compiler.compileComponentAsync(this._createDynamicComponent())
.then(factory => {
const injector = ReflectiveInjector.fromResolvedProviders([], this.vcRef.parentInjector);
this.vcRef.clear();
this.vcRef.createComponent(factory, 0, injector);
});
}

问题是 MyComponent 有一些 @InputOutput 绑定(bind)。可以在这里设置这个绑定(bind)吗?我怎样才能做到这一点?

最佳答案

输入和输出的绑定(bind)只能用于静态添加到另一个组件模板的组件。

在你的情况下,你可以命令式地做

 var cmpRef = this.vcRef.createComponent(factory, 0, injector);
cmpRef.instance.someInput = value;
cmpRef.instance.someOutput.subscribe(data => this.data = data);

关于 Angular 2 : Load component dynamically with parameters @Input and @Output,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39288727/

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