在ngFor中动态添加锚定名称组件
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在ngFor中动态添加锚定名称组件相关的知识,希望对你有一定的参考价值。
我正在开发一个Angular 2+应用程序,我正在尝试动态生成尽可能多的Material Datepicker(我将在FormArray中生成每个输入一个)。
<form [formGroup]="varDataForm" (ngSubmit)="onSubmit()" novalidate>
<div formArrayName="aggLevels"
*ngFor="let agLevel of varDataForm.get('aggLevels')?.controls; let aggLevelRow = index;">
<div [formGroupName]="aggLevelRow">
<label>{{agLevel?.get('aggregationLevelName')?.value}}</label>
<div formArrayName="variableDataForLevel"
*ngFor="let vardata of agLevel.get('variableDataForLevel')?.controls; let rowIndex = index;">
<div [formGroupName]="rowIndex">
<select formControlName="variableDataId">
<option *ngFor="let gs1 of gs1AIs" [value]="gs1.id">{{gs1.description}}</option>
</select>
<input formControlName="value" placeholder="Valor" [matDatepicker]="myDatepicker-{{rowIndex}}">
<mat-datepicker #myDatepicker-{{rowIndex}}></mat-datepicker>
<div class="error" *ngIf="vardata.get('value').hasError('required') && vardata.get('value').touched">
Obligatorio
</div>
<button type="button" class="btn" (click)="deleteRow(aggLevelRow, rowIndex)" [disabled]="disableDelete(aggLevelRow, rowIndex)">Borrar</button>
</div>
</div>
<button type="button" class="btn" (click)="addRow(aggLevelRow)" [disabled]="disableAdd()">Añadir</button>
</div>
</div>
<button *ngIf="varDataForm.get('aggLevels')?.controls?.length > 0" type="submit" class="btn btn-success" [disabled]="disableSubmit()">Guardar cambios</button>
</form>
我试过用:
<input formControlName="value" placeholder="Valor" [matDatepicker]="myDatepicker-{{rowIndex}}">
<mat-datepicker #myDatepicker-{{rowIndex}}></mat-datepicker>
但是我收到一个错误:
NodeInvocationException: Template parse errors:
Parser Error: Got interpolation ({{}}) where expression was expected at column 13 in [myDatepicker-{{rowIndex}}] in ng:///AppModuleShared/VarDataComponent.html@24:59 ("scription}}</option>
</select>
<input formControlName="value" placeholder="Valor" [ERROR ->][matDatepicker]="myDatepicker-{{rowIndex}}">
<mat-datepicker #myDatepicker-{{rowIndex}}></ma"): ng:///AppModuleShared/VarDataComponent.html@24:59
但我不能用一个以上的输入。
你知道为每个<mat-datepicker #myDatepicker></mat-datepicker>
生成<input formControlName="value" placeholder="Valor" [matDatepicker]="myDatepicker">
的方法吗?
答案
期望您的代码中没有其他错误,您不需要创建动态模板引用变量,它应该在迭代中正常工作,因此请尝试:
<input formControlName="value" [matDatepicker]="myDatepicker">
<mat-datepicker #myDatepicker></mat-datepicker>
以上是关于在ngFor中动态添加锚定名称组件的主要内容,如果未能解决你的问题,请参考以下文章