I have just started using reactive forms. The use case is that I get my filters from an API, then I dynamically build up my form controls like this. The constructor:
constructor(
private fb: FormBuilder,
) {
this.typesArray = this.fb.group({});
this.form = this.fb.group({
typesArray: this.typesArray,
});
}
// a bit further I do this after finishing an API call:
response.forEach((type: any) => {
(this.form.get('typesArray') as FormGroup).addControl(type.id, new FormControl(false));
});
This results in a row of checkboxes:
<div *ngFor="let Type of objectKeys(typesArray.controls)">
<ion-checkbox class="ion-no-margin" type="checkbox" [formControlName]="Type"></ion-checkbox>
<ion-label class="ion-no-margin" class="ml-2">**HERE I WANT THE LABEL**</ion-label>
</div>
The value is binded by [formControlName], which is the type Id. Is there a way to also add the label (type.title for example "Book", "DVD") to the form controls?
I would like to prevent having to build up a separate array with my values and labels just to show the correct label.