【发布时间】:2019-02-18 02:20:49
【问题描述】:
我正在使用 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 中的 References 组件。
我尝试使用 @ViewChildren('#fieldcmp') 字段列表:查询列表;和 @ContentChildren('#fieldcmp') 字段列表:查询列表; 在父组件内部,但如果我尝试在 ngafterViewInit 中访问,我没有得到任何元素
ngAfterViewInit() {
this.fieldsList.forEach((child) => { /* some logic....*/});
}
有人可以帮助我吗? 谢谢
-----------更新 -----------
使用@ViewChildren('fieldcmp') 修复后,我有一个ElementRef 列表,而不是我的AppComponent。 我投了它,所有的工作
this.filedsList
.forEach((child) => { atLeastOneRequired = atLeastOneRequired || (<ReusableFieldComponent> child).isRequired();
});
谢谢你的帮助
【问题讨论】:
-
我认为应该只是
@ViewChildren('fieldcmp')(没有#符号,只在模板中使用)
标签: angular angular6 ngfor viewchild ng-container