我有几个需要验证的输入字段,error-msg
当我分配本地模板变量(#requireSomeProperty1
)并检查它们(*ngIf="requireSomeProperty1.errors"
)时,它可以与我的自定义组件一起正常工作
<input type="hidden" [(ngModel)]="activeItem.SomeProperty1" name="SomeProperty1" #requireSomeProperty1="ngModel" [required]="someConditions1"/>
<error-msg *ngIf="requireSomeProperty1.errors" message="Property1 is required"></error-msg>
<input type="hidden" [(ngModel)]="activeItem.SomeProperty2" name="SomeProperty2" #requireSomeProperty2="ngModel" [required]="someConditions2"/>
<error-msg *ngIf="requireSomeProperty2.errors" message="Property2 is required"></error-msg>
现在,我需要从数据库中加载它们,而不是将它们硬编码为 html 模板。一切正常,但我不确定如何在使用*ngFor
.
我试过这个,但我得到了解析错误:
<div *ngFor="let item of itemsFromDB; index as i">
<input type="hidden" [(ngModel)]="activeItem[item.SomePropertyName]" [name]="item.SomePropertyName" #require_{{item.SomePropertyName}}="ngModel" [required]="item.SomeConditions">
<error-msg *ngIf="require_{{item.SomePropertyName}}.errors" [message]="item.ErrorMessage"></error-msg>
</div>
我也试过这个,但我也遇到了错误:
<div *ngFor="let item of itemsFromDB; index as i">
<input type="hidden" [(ngModel)]="activeItem[item.SomePropertyName]" [name]="item.SomePropertyName" #require_item.SomePropertyName="ngModel" [required]="item.SomeConditions">
<error-msg *ngIf="require_item.SomePropertyName.errors" [message]="item.ErrorMessage"></error-msg>
</div>
我也试过这个,我没有得到任何错误,但什么也没发生:
<div *ngFor="let item of itemsFromDB; index as i">
<input type="hidden" [(ngModel)]="activeItem[item.SomePropertyName]" [name]="item.SomePropertyName" #require+item.SomePropertyName="ngModel" [required]="item.SomeConditions">
<error-msg *ngIf="require+item.SomePropertyName.errors" [message]="item.ErrorMessage"></error-msg>
</div>
那么,我错过了什么?还是我想在这里做不可能的事情?