Hiện tại tôi đang tải một số thành phần của mình động với đoạn mã này.Góc 2: Tải thành phần động với các tham số @Input và @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);
});
}
Vấn đề là có một số MyComponent
@Input
và Output
bindings. Có thể thiết lập các ràng buộc này ở đây không? Làm thế nào tôi có thể đạt được điều đó?
Với RC6 + hiện nay là '.compileComponentAsync' .. đánh dấu vào một tùy chọn ở đây http://stackoverflow.com/q/38888008/1679310 * (còn với đầu vào liên kết) * –
@ RadimKöhler 'compileComponentAsync' đã bị xóa trong RC6. Với RC6 + bây giờ có 'compileModuleAndAllComponentsAsync' – yurzui
@yurzui, đúng .. đó là những gì tôi đang nói –