I have a form where I would like the user to edit the magazine subscriptions that he would like to receive. The code is as follows:
component:
export class OrderFormComponent {
subscriptions = [
{id: 'weekly', display: 'Weekly newsletter'},
{id: 'monthly', display: 'Monthly newsletter'},
{id: 'quarterly', display: 'Quarterly newsletter'},
];
mySubs = [
this.subscriptions[1]
]
order = new FormGroup({
subs: new FormArray(this.mySubs.map(sub => new FormControl(sub)), Validations.required)
});
}
Template:
<form [formGroup]="order">
<div formArrayName="subs">
<label>Sign me up for newsletters</label>
<p *ngFor="let s of subscriptions; let i=index">
<input type="checkbox" [value]="s.id" [formControlName]="i" /> {{ s.display }}
</p>
</div>
<div>
<input type="checkbox" formControlName="agree" /> I agree to the terms and conditions.
</div>
{{ order.value | json }}
When I launch the application, three flags are displayed, but only one of them is checked (in this case, erroneous). The one who checked has a label, while the others do not.

What am I doing wrong here?
source
share