I have a form that I am trying to dynamically add controls to using reactiveForms.
Within my code, I am trying to populate a dropdown menu. I am using formArray
since these are dynamic fields. The
Data:
{
"ruleName": "",
"ruleDescription": "",
"ruleOutcome": "",
"addVariable": "2",
"variables": [
{
"id": "2",
"name": "Device Trust Score",
"operator": [
{
"name": "Greater Than <",
"id": 3
},
{
"name": "Less Than >",
"id": 4
}
],
"values": ""
}
]
}
HTML:
<tbody formArrayName="variables">
<tr *ngFor="let variable of addRuleForm.get('variables').controls; let i=index" [formGroup]="variable">
<td>{{ addRuleForm.controls.variables.controls[i].controls.name.value}}
<input type="hidden" formControlName="id" value="addRuleForm.controls.variables.controls[i].controls.id.value" [attr.id]="'id'+i">
</td>
<td>
<select class="form-control input-sm" formControlName="operator" [attr.id]="'operator'+i">
<option value="">Select an Operator</option>
<option *ngFor="let o of addRuleForm.controls.variables.controls[i].controls.operator.value" value="{{ o.id }}">{{ o.name }}</option>
</select>
</td>
<td>
<button type="button" class="btn btn-danger btn-sm" (click)="removeVariable(v.id)"><i class="fa fa-trash-o"></i>
</button>
</td>
</tr>
</tbody>
This select input gets rendered to the page just fine. I inspect the source and see that the value has also been set to the ID correctly.
However, as soon as I select an option from it, I get this error:
Error: Error trying to diff '4'. Only arrays and iterables are allowed
referencing the ID of the item I selected.
Any thoughts?