Error:AppComponent.html:16 ERROR TypeError: Cannot read property 'controls' of undefined at Object.eval [as updateDirectives] (AppComponent.html:16)
app.component.ts:
import { Component, OnInit } from '@angular/core';
import { FormGroup, FormControl, FormArray } from '@angular/forms'
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
title = 'app';
signupForm: FormGroup;
arr;
ngOnInit() {
this.arr = ["id", "name", "weight", "quantity"];
this.signupForm = new FormGroup({
'id': new FormArray([new FormControl('1'), new FormControl('2')]),
'name': new FormArray([new FormControl('Beans'), new FormControl('Soup')]),
'weight': new FormArray([new FormControl('100'), new FormControl('125')]),
'quantity': new FormArray([new FormControl('60'), new FormControl('20')])
});
console.log(this.arr[0]);
}
}
app.component.html
<div class="container">
<div class="row">
<form [formGroup]="signupForm">
<div class="form-group">
<table class="table">
<thead>
<tr>
<td>S.no</td>
<td>NAME</td>
<td>WEIGHT</td>
<td>QUANTITY</td>
</tr>
</thead>
<tbody>
<div *ngFor="let i of arr">
<tr>
<td>
<div *ngFor="let val of signupForm.controls.i.controls">{{val.value}}</div>
</td>
</tr>
</div>
</tbody>
</table>
</div>
<button class="btn btn-primary" type="submit">Submit</button>
</form>
</div>
</div>
How to fetch the i value and pass it in *ngFor="let val of signupForm.controls.i.controls
signupForm.controls[i]controls, I assume.signupForm.controls[i].controls