目前我正在用这段代码动态加载我的一些组件.
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);
});
}
Run Code Online (Sandbox Code Playgroud)
问题是MyComponent有一些@Input和Output绑定.是否可以在此处设置此绑定?我怎样才能做到这一点?
Gün*_*uer 11
对输入和输出的绑定只能用于静态添加到另一个组件模板的组件.
在你的情况下,你可以这样做
var cmpRef = this.vcRef.createComponent(factory, 0, injector);
cmpRef.instance.someInput = value;
cmpRef.instance.someOutput.subscribe(data => this.data = data);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6632 次 |
| 最近记录: |