Dynamic form arrays

Note that phones is an array, potentially allowing for many inputs. We can implement this by building a FormArray with this.formBuilder.array and with several helper functions:

src/app/user/profile/profile.component.ts...  phones: this.formBuilder.array(this.buildPhoneArray(user ? user.phones : [])),...  private buildPhoneArray(phones: IPhone[]) {    const groups = []    if (!phones || (phones && phones.length === 0)) {      groups.push(this.buildPhoneFormControl(1))    } else {      phones.forEach(p => {        groups.push(this.buildPhoneFormControl(p.id, p.type, p.number))      })    }    return groups  }  private buildPhoneFormControl(id, type?: string, number?: string) {    return this.formBuilder.group({      id: [id],      type: [type || '', Validators.required], number: ...

Get Angular 6 for Enterprise-Ready Web Applications now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.