I'm trying to make a reactive form that displays a checkbox list where each item in the list is an item from an array. I am trying to make it so I can select as many items from the array using this checkbox list.
I have tried using FormArrays, reading multiple articles and StackOverflow answers on the topic but could not figure it out and have gotten very confused as different sources offered different solutiuons.
Here is my component.ts file:
export class ItemSelectorComponent implements OnInit {
public list: Array<ItemData>;
public collectionForm: FormGroup;
constructor(
private db: DatabaseService,
private fb: FormBuilder
) { }
async ngOnInit(): Promise<void> {
await this.getList();
this.collectionForm = this.fb.group({
name: [null],
items: [null] //I have tried here new FormArray([]) and this.fb.array(...) but to no avail
});
}
async getList() { //Sets the value of the list to response from the backend.
var res = await this.db.getList();
if (res.status == 200) {
this.list = res.body;
} else {
console.log(`List Status Code ${res.status}.`);
this.list = [];
}
}
//Form Functions
submit() {
console.log(this.collectionForm.value);
}
}
and my component.html
<form [formGroup]="collectionForm" (ngSubmit)="submit()">
<mat-form-field appearance="fill" required>
<mat-label>Collection Name</mat-label>
<input matInput formControlName="name">
</mat-form-field>
<br>
<!-- Implement here some sort of ngFor that loops over list and has checkbox inputs -->
<br>
<button mat-raised-button color='primary'>Submit</button>
</form>