我使用 ng-container 迭代列表并创建组件
<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>
我想列出 ng-container 内的引用组件。
我尝试使用
@ViewChildren('#fieldcmp') fieldsList: QueryList;和
@ContentChildren('#fieldcmp') fieldsList: QueryList;
在父组件内部,但如果我尝试访问 ngafterViewInit ,我不会得到任何元素
ngAfterViewInit() {
this.fieldsList.forEach((child) => { /* some logic....*/});
}
有人可以帮助我吗?
谢谢
------------Update-----------------------
使用 @ViewChildren('fieldcmp') 修复后,我有一个列表元素参考而不是我的 AppComponent。
我用所有的工作来铸造它
this.filedsList
.forEach((child) => { atLeastOneRequired = atLeastOneRequired || (<ReusableFieldComponent> child).isRequired();
});
谢谢你的帮助
只需删除#
从你的签名ViewChildren
选择器 - 仅在模板中使用:
@ViewChildren('fieldcmp') fieldsList: QueryList;
或者您可以使用组件类作为选择器:
@ViewChildren(HelloComponent) fieldsList: QueryList<HelloComponent>;
在这里工作 stackblitz:https://stackblitz.com/edit/angular-xeudlp https://stackblitz.com/edit/angular-xeudlp
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)