I have a set of categories that is coming from a webservice: categories
, and an array with selected categories: selectedCategories
To display all the checkboxes, I do:
<form [formGroup]="formGroup" (ngSubmit)="onSubmit()">
<div class="row">
<div *ngFor="let category of categories">
<input type="checkbox" class="custom-control-input" [id]="category.id" formControlName="category"
[checked]="categoriesSelected?.includes(category.id)">
<label class="custom-control-label" [for]="category.id">{{ category.name | translate }}</label>
</div>
</div>
</form>
this works great.
Then I build my formGroup:
ngOnInit() {
this.categoriesSelected = this.championships.map(championship => championship.category.id);
this.formGroup = this.formBuilder.group({
categories: this.formBuilder.array([{}])
});
}
Now, the output I need is just the ids of the selected categories.
I am a bit lost about linking the the formBuilder and the formGroup.
If I add formControlName="categories"
in my template, in the input
tag, all the data disappears.
How should I do it ?