Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular4 compile error on production build: "Property controls does not exist on type AbstractControl"

My code builds and runs perfectly in dev mode. When I build for production I get this error on this line:

        <div *ngFor="let child of form.controls.emailsArray.controls; let i=index">

The form is built like this:

  public form: FormGroup;
  private control: FormArray;
  private emailsModel = { emails: ['','','','','']}  // the model, ready to hold the emails
  private fb : FormBuilder;

  constructor() {}

  ngOnInit() {
    this.fb = new FormBuilder;
        this.form = this.fb.group({
      emailsArray: this.fb.array([])
    });
    this.control = <FormArray>this.form.controls['emailsArray'];
    this.patch();    
  }

  private patch(): void {
    // iterate the object model and extra values, binding them to the controls
    this.emailsModel.emails.forEach((item) => {
      this.control.push(this.patchValues(item));
    })
  }

  private patchValues(item: string): AbstractControl {
    return this.fb.group({
      email: [item, Validators.compose([emailValidator])] 
    })
  }

So how do I reference these child controls in html?

Note: similar to this question, except my error occurs in HTML, not typescript.

like image 516
rmcsharry Avatar asked Jan 29 '23 12:01

rmcsharry


1 Answers

I'm not sure if this is good practice, but changing the HTML to this worked:

<div *ngFor="let child of form.controls.emailsArray['controls']; let i=index">
like image 154
rmcsharry Avatar answered Feb 13 '23 05:02

rmcsharry