Having fully digested the Angular Reactive forms docs, I'm still struggling to implement the following functionality:
Using a list of Divisions from my NgRx store, output a mat-checkbox for each with the division name and it's checked status (using division.current property - so that the currently active divisions are checked by default), within an Angular reactive form.
Here's where I'm at:
new-season-component.ts
export class NewSeasonComponent implements OnInit {
chooseDivisions: FormGroup;
divisions: FormArray;
allDivisions: Division[];
constructor(
private fb: FormBuilder,
private store: Store<AppState>
) {
}
ngOnInit() {
this.store.dispatch(new AllDivisionsRequested());
this.store
.pipe(
select(selectAllDivisions)
).subscribe(divisions => this.allDivisions = divisions);
this.chooseDivisions = this.fb.group({
divisions: this.fb.array([])
});
this.addDivisions();
}
createDivisionFormControl(division: Division): FormGroup {
return this.fb.group({
id: division.id,
name: division.name,
current: division.current
});
}
addDivisions(): void {
this.divisions = this.chooseDivisions.get('divisions') as FormArray;
this.allDivisions.map(
division => {
this.createDivisionFormControl(division);
}
);
}
In summary - grabbing divisions from the store, creating a formGroup containing an empty formArray. Then running a method to map over the divisions and call another method to create them as formGroups.
** new-season-component.html**
<form [formGroup]="chooseDivisions">
<ng-template matStepLabel>Choose which teams are part of this season</ng-template>
<div formArrayName="divisions">
<div *ngFor="let division of chooseDivisions.get('divisions').controls; let i = index;">
<div [formGroupName]="i">
<mat-checkbox formControlName="division">{{ division[i].name }}</mat-checkbox>
</div>
</div>
</div>
<div>
<button mat-button matStepperNext>Next</button>
</div>
</form>
I've used various online material to get to this point - included this article but none seem to quite be what I'm looking for - ie adding a list of divisions as checkboxes on load, rather than the functionality to click a button to add a new blank form control.
I'm obviously doing something fundamentally wrong. Should I even be using a reactive form here?
division
inside the forms groups in the form array. The form controls are named id, name and current. SoformControlName="division"
is not correct. – JB Nizetdivision[i]
is also incorrect: division is a FormGroup. – JB Nizet