我的模板上有很多樣板代碼,它們大致遵循這種結構。如何動態命名模板引用變量?
<div class="form-group col-lg-6"
[ngClass]="{'has-error': errors?.startDate || !startDate.valid}">
<label>Start Date</label>
<input class="form-control"
[(ngModel)]="currentEntity.startDate"
#startDate="ngModel" name="startDate"
required />
<small *ngIf="!startDate.valid" class="help-block">
Start Date Is Required
</small>
<small *ngIf="errors?.startDate" class="help-block">
{{errors.startDate}}
</small>
</div>
第二個錯誤塊用於返回的任何服務器驗證。
我曾希望做一個指令,可能只是像<validatedInput [inputName] = "startDate" />
這樣的東西,並替換每個屬性中的關鍵元素。無論如何動態地改變像#startDate="ngModel"
到#{{inputName}}="ngModel"
。 ngIf也必須動態設置。
我正在運行「@ angular/forms」:「0.1.1」。
查看Angular 2 formbuilder。利用這一點,我認爲你可能能夠實現你正在尋找的東西。 – hholtij
模板變量是純靜態的。你不能動態地命名它們。 –