В настоящее время я загружаю некоторые мои компоненты динамически с помощью этой части кода.Угловой 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, правильно .. вот что я говорю –