Change the loadComponent function to this:
loadComponent() {
let viewContainerRef = this.adHost.viewContainerRef;
for (const ad of this.ads) {
let componentFactory = this.componentFactoryResolver.resolveComponentFactory(ad.component);
let componentRef = viewContainerRef.createComponent(componentFactory);
(<AdComponent>componentRef.instance).data = ad.data;
}
}
The viewContainer inserts the views one after the other.
EDIT:
About the error thrown. It's better with a piece of code.
And here's a more thorough explanation of the problem:
https://blog.angularindepth.com/everything-you-need-to-know-about-the-expressionchangedafterithasbeencheckederror-error-e3fd9ce7dbb4
import { ChangeDetectorRef, (...) } from '@angular/core';
constructor(
private cd: ChangeDetectorRef,
private componentFactoryResolver: ComponentFactoryResolver
) { }
ngAfterViewInit() {
this.loadComponent();
this.getAds();
this.cd.detectChanges();
}