目前我正在使用這段代碼動態加載一些組件。Angular 2:使用參數@Input和@Output動態加載組件
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
有一些@Input
和Output
綁定。這裏可以設置這個綁定嗎?我怎樣才能做到這一點?
隨着RC6 +現在有'.compileComponentAsync' ..點擊這裏一個選項http://stackoverflow.com/q/38888008/1679310 *(也與綁定輸入)* –
@RadimKöhler'compileComponentAsync'在RC6中被刪除。使用RC6 +現在有'compileModuleAndAllComponentsAsync' – yurzui
@yurzui,正確..這就是我所說的 –