[英]Angular - How to create dynamic template variable inside NgFor
Can someone help me how to create dynamic template reference variable inside ngFor, below is my code有人可以帮我如何在 ngFor 中创建动态模板引用变量,下面是我的代码
Here I wanted to use [matAutocomplete]="auto" with mat-autocomplete #auto="matAutocomplete" dynamically based on my ngFor在这里,我想根据我的 ngFor 动态使用 [matAutocomplete]="auto" 和 mat-autocomplete #auto="matAutocomplete"
<div class="col" *ngFor="let item of formItems;">
<mat-form-field>
<input type="text" placeholder="{{item.label}}" aria-label="Number" matInput
formControlName={{item.type}} [matAutocomplete]="auto" id="item.type">
<mat-autocomplete #auto="matAutocomplete" (optionSelected)='updateData(item.type)'>
<mat-option *ngFor="let option of item.filterdData | async" [value]="option">
{{option}}
</mat-option>
</mat-autocomplete>
</mat-form-field>
</div>
</div>
This will definitely work for you.这肯定对你有用。
<div class="col" *ngFor="let item of formItems; index as i;let j=index">
<mat-form-field>
<input type="text" placeholder="{{item.label}}" aria-label="Number" matInput
formControlName={{item.type}} [matAutocomplete]="i" id="item.type">
<mat-autocomplete #i="matAutocomplete" (optionSelected)='updateData(item.type)'>
<mat-option *ngFor="let option of item.filterdData | async" [value]="option">
{{option}}
</mat-option>
</mat-autocomplete>
</mat-form-field>
</div>
Let me know in comment if you don't see this solution useful.如果您认为此解决方案没有用,请在评论中告诉我。 If formContolName does not work then try replacing it with [value]如果 formContolName 不起作用,请尝试将其替换为 [value]
<input type="text" placeholder="{{item.label}}" aria-label="Number" matInput
[value]="item.type" [matAutocomplete]="i" id="item.type">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.