我有多个在ngFor循环中生成的角度形式,如下所示。每个表格都是一个向导步骤,用户可以通过单独填写表格来前进。在表单输入字段中,我添加了自定义属性,该属性通过动态建筑物名称保存错误消息。我正在努力的是在单击 Next 时显示与 formControl 相关的错误消息,因为我无法从 FormControl 中获取 Native 元素。
<div *ngFor="let building of buildings;let i = index" >
<form #childForms="ngForm" name="{{building.id}}" id="{{building.id}}" >
<input [attr.errorLabel]="'please name of the ' + building.Class " [name]="i + 'buildingName'" type="text" [(ngModel)]="building.Name" class="text-iput" [required]="true">
</form>
</div>
<button styleClass="action" label="Next" (onClick)="goNext(activeStep)"></button>
下面是我如何尝试从 TS 文件中获取当前步骤并尝试手动调用验证
activeStep : number = 0;
@ViewChildren('childForms', {read: NgForm}) childForms: QueryList<NgForm>;
goNext(activeStep) {
errors = [];
for (const name in this.childForms[activeStep].controls) {
if (this.childForms[activeStep].controls[name].invalid) {
// How Can I get the ATTRIBUTE associated with the form control here
// any way that i can get the native element of the Form Control
// in my case [attr.errorLabel]="'please name of the ' + building.Class "
}
}
}