简体   繁体   English

如何动态设置 Angular # 模板参考变量

[英]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.

相关问题 如何在* ngFor中设置唯一的模板引用变量? (角度) - How to set unique template reference variables inside an *ngFor? (Angular) Angular 4 模板参考变量 - Angular 4 template reference variables 如何动态访问 angular.html 中定义的模板引用变量(符号:#) - how to dynamically access template reference variables (symbol: #) defined in the angular.html Angular-如何在组件模板中引用类变量? - Angular - How to reference class variables in a component template? 如何在 function 中动态设置模板引用 - How to set template reference dynamically inside function Angular 2:如何访问动态创建的模板变量 - Angular 2: How to access dynamically created template variables 如何使用 Angular 中的模板参考动态渲染 ng-template - How to render ng-template dynamically with Template Reference in Angular angular 2模板不识别模板引用变量 - angular 2 template doesnt recognize template reference variables 如何动态地在angular中分配模板参考变量 - How can I dynamically assign a template reference variable in angular 如何在循环内的 Angular 模板上的 div 中动态命名引用 - How to name dynamically a reference in a div on Angular template inside a loop
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM