Angular Reactive Forms: working with 'how to read property of undefined' When Adding Dynamic Controls
I'm stuck trying to I'm writing unit tests and I'm not sure how to approach I'm deploying to production and This might be a silly question, but Quick question that's been bugging me - I'm working with an scenario with Angular's reactive forms while trying to dynamically add controls to a form group....... I'm using Angular 15, and I have a scenario where I need to allow users to add multiple addresses. Each address should have fields for street, city, and zip code. However, when I attempt to add a new address, I encounter the behavior: `want to read property 'controls' of undefined`. Here's what I've tried so far. My component looks something like this: ```typescript import { Component } from '@angular/core'; import { FormBuilder, FormGroup, FormArray } from '@angular/forms'; @Component({ selector: 'app-address-form', templateUrl: './address-form.component.html', }) export class AddressFormComponent { addressForm: FormGroup; constructor(private fb: FormBuilder) { this.addressForm = this.fb.group({ addresses: this.fb.array([]) }); } get addresses(): FormArray { return this.addressForm.get('addresses') as FormArray; } addAddress() { const addressGroup = this.fb.group({ street: [''], city: [''], zip: [''] }); this.addresses.push(addressGroup); } } ``` In the template, I'm using `*ngFor` to loop through the `addresses` array: ```html <form [formGroup]='addressForm'> <div formArrayName='addresses'> <div *ngFor='let address of addresses.controls; let i = index' [formGroupName]='i'> <input formControlName='street' placeholder='Street'> <input formControlName='city' placeholder='City'> <input formControlName='zip' placeholder='Zip'> </div> </div> <button (click)='addAddress()'>Add Address</button> </form> ``` When I click the 'Add Address' button, it throws the behavior on the `*ngFor` line. I verified that the `addresses` array is initialized correctly, and I've checked all the variable names multiple times. I also tried using `this.addresses.push()` inside a timeout to see if it was a timing scenario, but that didn't resolve the question. I would appreciate any insights into what might be going wrong or how I can avoid this behavior when dynamically adding form controls. I'm working on a CLI tool that needs to handle this. What's the best practice here? This is my first time working with Typescript LTS. Am I missing something obvious? This is my first time working with Typescript 3.10. This issue appeared after updating to Typescript latest. What are your experiences with this? I'm developing on Windows 11 with Typescript. The stack includes Typescript and several other technologies. Could someone point me to the right documentation? I'm on Windows 10 using the latest version of Typescript.