[英]How to Dynamically Set Angular # Template Reference Variables
I have a list of groups populated from a database and use ngFor to iterate through in the html.我有一个从数据库填充的组列表,并使用 ngFor 在 html 中迭代。
<mat-card *ngFor="let group of groups" >
<mat-card-title #group > {{group}} </mat-card-title>
</mat-card>
I would like the #group to be the value of the group, not hardcoded.我希望#group成为组的值,而不是硬编码。 That way I can have another component to scroll to the different template variables.
这样我就可以让另一个组件滚动到不同的模板变量。
How can I do this?我怎样才能做到这一点? Thanks.
谢谢。
As per my knowelage template reference variables are uniquely scoped to the templates that they are defined in. And structural directive creates a nested template and therefore, introduces a separate scope, in your case mat-card-title
.根据我的知识模板引用变量的唯一范围是它们定义的模板。结构指令创建一个嵌套模板,因此,在您的情况下引入了一个单独的范围
mat-card-title
。
So basically using group template reference variable inside <mat-card-title #group >
will do in your case since it will hold unique scope for itself.因此,基本上在
<mat-card-title #group >
使用组模板引用变量将适用于您的情况,因为它将为自己保留唯一的范围。
<mat-card *ngFor="let group of groups" >
<mat-card-title #group > {{group}} </mat-card-title>
</mat-card>
For more details please refer answer by @yurzui here
有关更多详细信息,请参阅@yurzui 的回答在这里
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.