typescript 在Angular中使用HTML模板内“QueryList”的"find“方法

k3bvogb1  于 2023-03-04  发布在  TypeScript
关注(0)|答案(1)|浏览(157)

有没有办法在HTML模板中使用QueryList上的方法?
例如,在TS文件中,我可以有:

@ContentChildren(DonneeEntiteDirective) content!: QueryList<DonneeEntiteDirective>
let test = this.content.find(e => e.name === 'surface');

但由于某种原因,HTML模板中的相同内容不起作用:

<ng-container [ngTemplateOutlet]="content.find(e => e.name === 'surface')"></ng-container>

我唯一能做到的方法就是这样做:

<ng-container *ngFor="let template of content">
          <ng-container *ngIf="template.name === column" [ngTemplateOutlet]="template.templateRef"></ng-container>
</ng-container>

有没有更好的办法?

xytpbqjk

xytpbqjk1#

是的,模板中确实不允许使用箭头函数。
您可以将subscribe添加到您的QueryList.changes()中,并且您的组件具有一个属性,该属性就是您要查找的ContentChild

相关问题