title: Angular 5 add, remove row on click buttons
body: <p>In angular 5 I want to add new row and remove new row button so that thy will add a new row to the table and delete the row when click on the buttons. But when I am trying this code its showing error like </p>
  
  <pre><code>ERROR TypeError: Cannot read property 'errors' of undefined
  </code></pre>
  
  <p>Here is what I have tries</p>
  
  <p>event-create.component.html looks like this</p>
  
  <pre><code>&lt;div class="container col-md-12"&gt;
    &lt;h1 class="page-header"&gt;Create Event&lt;/h1&gt;
    &lt;form [formGroup] = "form" (ngSubmit)="onEventSubmit()"&gt;
    &lt;fieldset&gt;
      &lt;div class="form-group"&gt;
        &lt;label for="eventname"&gt;Event Name&lt;/label&gt;
        &lt;div class='form-group'&gt;
          &lt;input type="text" class="form-control" autocomplete="off" placeholder="Event Name" formControlName="eventname"&gt;
        &lt;/div&gt;
      &lt;/div&gt;
  
      &lt;div class="form-group"&gt;
        &lt;label for="eventdesc"&gt;Event Description&lt;/label&gt;
        &lt;div class='form-group'&gt;
          &lt;input type="text" class="form-control" autocomplete="off" placeholder="Event Description" formControlName="eventdesc"&gt;
        &lt;/div&gt;
      &lt;/div&gt;
  
      &lt;h4&gt;Event Package&lt;/h4&gt;
      &lt;hr&gt;
      &lt;div class="row" formGroupName="itemRows"&gt;
        &lt;div class="form-group col-md-4"&gt;
          &lt;label for="packagename"&gt;Package Name&lt;/label&gt;
          &lt;input type="text" class="form-control" autocomplete="off" placeholder="Package Name" name="packagename" &gt;
        &lt;/div&gt;
        &lt;div class="form-group col-md-2"&gt;
          &lt;label for="packageprice"&gt;Package Price&lt;/label&gt;
          &lt;input type="number" class="form-control" autocomplete="off" placeholder="Package Price" name="packageprice" &gt;
        &lt;/div&gt;
        &lt;div class="form-group col-md-2"&gt;
          &lt;label for="packagelimit"&gt;Max Purchase Limit&lt;/label&gt;
          &lt;input type="number" class="form-control" name="packagelimit" autocomplete="off" &gt;
        &lt;/div&gt;
        &lt;div class="form-group col-md-2"&gt;
          &lt;br/&gt;
          &lt;input type="button" (click)="addPackageRow()" class="btn btn-md btn-success" value="Add New Package" name=""&gt;
        &lt;/div&gt;
      &lt;/div&gt;
      &lt;input type="submit" class="btn btn-primary" value="Submit"&gt;
  
    &lt;/fieldset&gt;
  &lt;/form&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>event-create.component.ts looks like this</p>
  
  <pre><code>import { Component, OnInit } from '@angular/core';
  import { FormBuilder, FormGroup, Validators } from '@angular/forms';
  import { FormControlName } from '@angular/forms/src/directives/reactive_directives/form_control_name';
  
  @Component({
    selector: 'app-event-create',
    templateUrl: './event-create.component.html',
    styleUrls: ['./event-create.component.css']
  })
  export class EventCreateComponent implements OnInit {
  
    form :  FormGroup;
  
    constructor(
      private formBuilder : FormBuilder,
      ) { this.createEventForm(); }
  
    createEventForm() {
      this.form = this.formBuilder.group({
        eventname: ['', Validators.compose([
          Validators.required,
          Validators.minLength(5)
        ])],
        eventdesc: ['', Validators.required],
        packagename: ['',],
        packageprice: ['',],
        packagelimit: ['',]
      })
    }
  
    ngOnInit() {
      this.form = this.formBuilder.group({
          itemRows: this.formBuilder.array([this.initItemRows()])
        });
    }
  
    initItemRows() {
          return this.formBuilder.group({
              itemname: ['']
          });
      }
  
    public addPackageRow() {
      console.log(this.form);
       // const control = this.form.controls['itemRows'];
      // control.push(this.initItemRows());
    }
  
  }
  </code></pre>
  
  <p>I don't know what is going wrong here. Can someone tell me how to make this work? Anu help and suggestions will be really appreciable.</p>