简体   繁体   English

Angular 4 模板参考变量

[英]Angular 4 template reference variables

is it possible to have a dynamic template reference variable?是否有可能有一个动态模板引用变量?

for example, i would like to do something like this:例如,我想做这样的事情:

 <tr *ngFor="let item of items.controls; let i=index" >
  <th >{{i}}</th>
  <td>

    <mat-form-field>
      <input matInput matDatepicker="{{'pick'+i}}" placeholder="date" 
      formControlName="date">

      <mat-datepicker-toggle matSuffix for="{{'pick'+i}}">
      </mat-datepicker-toggle>

      <mat-datepicker #"{{'pick'+i}}"></mat-datepicker>

    </mat-form-field>

  </td>
 </tr>

I am trying to figure out how to write <mat-datepicker #"{{'pick'+i}}"></mat-datepicker> such that the reference variable is dynamic with the loop index.我想弄清楚如何编写<mat-datepicker #"{{'pick'+i}}"></mat-datepicker>这样引用变量是动态的,循环索引。 Using interpolation here just to explain what I am trying to achieve.在这里使用插值只是为了解释我想要实现的目标。

regards Adisa问候阿迪萨

Template reference variable inside embedded view(*ngFor) has its own scope so you can just use the same name:嵌入视图(*ngFor) 中的模板引用变量有自己的作用域,因此您可以使用相同的名称:

<tr *ngFor...>
    ...
    <mat-form-field>
      <input matInput [matDatepicker]="pick" placeholder="date" formControlName="date">
      <mat-datepicker-toggle matSuffix [for]="pick"></mat-datepicker-toggle>
      <mat-datepicker #pick></mat-datepicker>
    </mat-form-field>

Example 例子

Template reference variables cannot be named dynamically.模板引用变量不能动态命名。 They must be statically analysable它们必须是静态可分析的

Have a look here where you have other suggestions on reaching the goal 在这里查看您对实现目标的其他建议

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM