У меня есть компонент, динамически загруженный в <ng-template>
.
Вот пример кода:
HTML:
<ng-template #dynamic></ng-template>
TS:
@ViewChild('dynamic', { read: ViewContainerRef })
dynamic: ViewContainerRef;
constructor(
private compiler: Compiler,
private injector: Injector
) {}
async ngAfterViewInit() {
const exampleModule = await import(PATH_TO_A_MODULE_TO_BE_LOADED).then(m => m.ExampleModule);
const moduleFactory = await this.compiler.compileModuleAsync(exampleModule);
const moduleRef = moduleFactory.create(this.injector);
const componentFactory = moduleRef.instance.resolveComponent();
const ref = container.createComponent(componentFactory, null, moduleRef.injector);
}
Приведенный выше код будет динамически загружать компонент в ExampleModule
.
Проблема в том, что компонент не определяет свои изменения автоматически.
Поэтому мне приходится вызывать this.changeDetectorRef.detectChanges()
каждый раз, когда что-то меняется в компоненте.
Я хотел бы знать, есть ли способ позволить компоненту автоматически обнаруживать изменения без использования файла ChangeDetectorRef
.