In my form I will have the following elements:
- input (checkbox)
- code (span)
- name (span)
These values come from the server, which is why it is added dynamically.
However, I face the following problem with my algorithm:
core.js: 6228 ERROR Error: Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays.
Only this error does not make sense, because the console is showing the values correctly and the guy is an Array:
MAIN COMPONENT
@Component({
selector: 'zx-create-country-form',
templateUrl: './create-country-form.component.html',
styleUrls: ['./create-country-form.component.scss'],
})
export class CreateCountryFormComponent implements OnInit {
createCountryForm!: FormGroup;
dataTemp = [
{
code: 'BR',
name: {
pt_BR: 'Brasil',
},
},
{
code: 'TST',
name: {
pt_BR: 'tst3',
},
},
];
constructor(protected readonly componentService: ComponentService, protected readonly formBuilder: FormBuilder) {
this.createCountryForm = formBuilder.group({
countries: formBuilder.array(this.addCountries()),
});
}
ngOnInit(): void {}
addCountries(): FormGroup[] {
const formsGroups: FormGroup[] = [];
this.dataTemp.map(country => {
formsGroups.push(
this.formBuilder.group({
radio: this.formBuilder.control([{ value: false, disabled: true }], [Validators.requiredTrue]),
code: this.formBuilder.control(country.code, [Validators.required, Validators.minLength(1), Validators.maxLength(2)]),
name: this.formBuilder.control(country.name.pt_BR),
})
);
});
return formsGroups;
}
}
CHILD COMPONENT
<zx-form [fmGroup]="fmGroup">
<div [formArrayName]="fmArrayName" *ngFor="let country of fmGroup.controls.countries; let i = index">
<div [formGroupName]="i">
<input formControlName="radio" placeholder="Item name" />
<input formControlName="code" placeholder="Item name" />
<input formControlName="name" placeholder="Item name" />
eqqe
</div>
</div>
</zx-form>