angular - 来自对象数组的角度多个输入
问题描述
我需要显示对象数组中的多个输入字段(本例中为 testtttt)。如果未填写输入字段,我需要Action ({{i +1}})
获得红色。如何从我的输入中获取测试有效状态?
我知道可以询问输入名称的有效状态(action.valid)
。但在这种情况下,该名称的名称中包含数字 i。
<div *ngFor="let test of testtttt; let i = index" layout="column" layout-wrap>
<div>
<div>
<span>Action ({{i + 1}}):</span>
<div>
<mat-label>
<mat-form-field>
<input matInput [(ngModel)]="componentTest[i].description" required name="action + {{i}}">
</mat-form-field>
</mat-label>
</div>
</div>
</div>
</div
我试过这个,但它给出了错误。
<span [ngClass]="{'red': !(action + i).valid}">Action ({{i + 1}}):</span>
解决方案
将模板引用变量添加到您的输入并使用它指定元素。
这是我的样子:
<div class="form-group"
[ngClass]="{'has-error': (firstNameVar.touched || firstNameVar.dirty) && !firstNameVar.valid }">
<label class="col-md-2 control-label"
for="firstNameId">First Name</label>
<div class="col-md-8">
<input class="form-control"
id="firstNameId"
type="text"
placeholder="First Name (required)"
required
minlength="3"
[(ngModel)]=customer.firstName
name="firstName"
#firstNameVar="ngModel" />
<span class="help-block" *ngIf="(firstNameVar.touched || firstNameVar.dirty) && firstNameVar.errors">
<span *ngIf="firstNameVar.errors.required">
Please enter your first name.
</span>
<span *ngIf="firstNameVar.errors.minlength">
The first name must be longer than 3 characters.
</span>
</span>
</div>
</div>
注意模板引用变量:#firstNameVar
.
(此示例不使用索引,但不应将索引作为模板引用变量的一部分。)
推荐阅读
- php - 无法添加外键约束 Laravel 6.x
- laravel - 电子邮件通知中的条件路由在 Laravel 中失败
- reactjs - 打字稿附加道具
- python-3.x - 正则表达式 (\w)\1{4,} 是什么意思 python
- javascript - 使用 AJAX 将 FormData 发送到 nodejs + express 服务器
- python - FastAPI 发现我的 JSON 对象数组是一个无效列表
- html - 如何根据选择选项的值启用或禁用输入使用 jQuery?
- powershell - PowerShell 从脚本创建脚本
- javascript - 如何在不影响 li 内部的其他元素的情况下编辑动态添加的 li 的内容 - jQuery?
- javascript - 如何在 Chrome Devtools JS 控制台中从“http://bar.org/some-esm-module.js”导入 blah?