I need to show users' names and a list of their courses. Different users have different numbers of courses. Thus, I am trying Angular Form Array.
Here is my .ts:
this.form = this.fb.group({
name: [''],
courses: this.fb.array([
this.fb.group({
name: [''],
})
]),
});
get courses() {
return this.form.controls["courses"] as FormArray;
}
and my .html:
<form [formGroup]="form" (ngSubmit)="onSubmit()">
<div class="mb-3">
<label>Name</label>
<input type="text" formControlName="name"/>
</div>
<div class="mb-3">
<label>Courses</label>
<div formArrayName="courses">
<div *ngFor="let course of courses.controls; let i = index">
<div [formGroupName]="i">
<input type="text" formControlName="name"/>
</div>
</div>
</div>
</div>
</form>
The problem is that a user has two courses but only one of the courses is shown! Does anyone know what is the problem?
P.s. Happy new year :)