我正在尝试构建一个表单,您可以在单击添加按钮时创建另一个输入字段。问题是为每一行输入填充了相同的值。从下图中可以看出,每一行都获得相同的值。我认为这是因为 ngmodel。或者也许是别的东西。如何解决这个问题?
html
<div class="row mt-2 " *ngFor="let auditField of dynamicAuditField; let i = index;">
<div class="col-md">
<select class="form-control dynamic-input form-input p-0 pl-2"
id="criteria" #criteria="ngModel" [(ngModel)]="addmodel.criteria"
name="criteria_{{auditField.id}}">
<option [value]='null' disabled>Select Criteria</option>
<option *ngFor="let data of auditField.criteria" [value]="data.value">
{{data.name}}</option>
</select>
</div>
<div class="col-md">
<select class="form-control dynamic-input form-input p-0 pl-2" id="condition" #condition="ngModel"
[(ngModel)]="addmodel.condition" name="condition_{{auditField.id}}">
<option [value]='null' disabled>Select Condition</option>
<option *ngFor="let data of auditField.condition" [value]="data.value">{{data.name}}</option>
</select>
</div>
<div class="col-md">
<input type="text" class="form-control dynamic-input form-input p-0 pl-2"
#value="ngModel" [(ngModel)]="addmodel.value" name="value_{{auditField.id}}">
</div>
<div>
<i class="fa fa-trash-o input-row-delete" aria-hidden="true (click)="removeField(i)"></i>
</div>
</div>
ts
public dynamicAuditField: any[] = [{
id: 1,
criteria: [
{
name: "Process 1",
value: "process_1"
},
{
name: "Process 2",
value: "process_2"
},
{
name: "Process 3",
value: "process_3"
},
],
condition: [
{
name: "Sub Process 1",
value: "sub_process_1"
},
{
name: "Sub Process 2",
value: "sub_process_2"
},
{
name: "Sub Process 3",
value: "sub_process_3"
},
],
value: ''
}];
addField(){
this.dynamicAuditField.push({
id: this.dynamicAuditField.length + 1,
criteria: [
{
name: "Process 1",
value: "process_1"
},
{
name: "Process 2",
value: "process_2"
},
{
name: "Process 3",
value: "process_3"
},
],
condition: [
{
name: "Sub Process 1",
value: "sub_process_1"
},
{
name: "Sub Process 2",
value: "sub_process_2"
},
{
name: "Sub Process 3",
value: "sub_process_3"
},
],
value: ''
})
}