I post my comment as an answer as requested by @OP.
There is a great example in the official angular documentation on how to create dynamic component. Basically it contains a component (AdBannerComponent) that can create component from a given list using ComponentFactoryResolver.
Based on this example we can create a new component that just reuse the AdBannerComponent:
import { Component, Input, ComponentFactoryResolver, ViewChild, OnInit } from '@angular/core';
import { AdComponent } from './ad.component';
import { AdItem } from './ad-item';
import { AdDirective } from './ad.directive';
@Component({
template: `
<div class="job-ad">
<app-ad-banner [ads]="ads"></app-ad-banner>
</div>
`})
export class HeroSubProfileComponent implements AdComponent, OnInit {
@Input() data: any;
ads: AdItem[];
constructor() { }
ngOnInit() {
this.ads = this.data;
}
}
This component will create dynamically the component given as an input in data. We can also directly reuse AdBannerComponent as is by updating it's definition to match AdItem:
export class AdBannerComponent implements OnInit, OnDestroy {
@Input() ads: AdItem[];
currentAdIndex = -1;
@ViewChild(AdDirective) adHost: AdDirective;
interval: any;
@Input() data: any;
constructor(private componentFactoryResolver: ComponentFactoryResolver) { }
ngOnInit() {
if (this.data != null) {
this.ads = this.data;
}
this.loadComponent();
this.getAds();
}
If we update the AdService (providing the list of component to create) with the following:
@Injectable()
export class AdService {
getAds() {
return [
new AdItem(HeroProfileComponent, {name: 'Bombasto', bio: 'Brave as they come'}),
new AdItem(HeroProfileComponent, {name: 'Dr IQ', bio: 'Smart as they come'}),
new AdItem(HeroJobAdComponent, {headline: 'Hiring for several positions',
body: 'Submit your resume today!'}),
new AdItem(HeroJobAdComponent, {headline: 'Openings in all departments',
body: 'Apply today'}),
// component using composition
new AdItem(HeroSubProfileComponent, [new AdItem(HeroProfileComponent, {name: 'Great Scott', bio: 'Awesome'})]),
// directly using AdBannerComponent
new AdItem(AdBannerComponent, [new AdItem(HeroProfileComponent, {name: 'Bombasto', bio: 'Brave as they come'})])
];
}
}
We are creating dynamic component inside another one and we can do it as deep as we want.
You can find a running example here https://stackblitz.com/edit/angular-dsivwn.
ContainerComponentjust like your sample. Now there is no circular reference and I am a happy man. Do you want to move this comment into an answer so I can accept it? Thanks again!