I'using ng-container to iterate on a list and create components
<ng-container *ngFor="let f of optionsList; let i = index;">
<!-- component-->
<app-component #fieldcmp *ngIf="isAppComponent(f)" ></app-field>
<!--another components-->
<app-anoter-component1 *ngIf="isAnotherComponent1(f)"> </app-anoter-component1>
...
<app-anoter-componentn *ngIf="isAnotherComponentn(f)"> </app-anoter-componentn>
</ng-container>
I would to list of References components inside ng-container.
I tried to use @ViewChildren('#fieldcmp') fieldsList: QueryList; and @ContentChildren('#fieldcmp') fieldsList: QueryList; inside father component but i get no elements if I try to acces in ngafterViewInit
ngAfterViewInit() {
this.fieldsList.forEach((child) => { /* some logic....*/});
}
Can someone can help me? Thanks
-----------Update -----------------------
after fix with @ViewChildren('fieldcmp') I have a list of ElementRef instead of my AppComponent. I cast it with and all work
this.filedsList
.forEach((child) => { atLeastOneRequired = atLeastOneRequired || (<ReusableFieldComponent> child).isRequired();
});
thank you to help
Just remove the
#
sign from yourViewChildren
selector - that is used only in templates:Or you could use component class as a selector:
Working stackblitz here: https://stackblitz.com/edit/angular-xeudlp