import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core'; import { FormBuilder, FormGroup, Validators, FormControl } from '@angular/forms'; @Component({ selector: 'cm-form-doc-5', templateUrl: './form-doc-5.component.html', styleUrls: ['./form-doc-5.component.css'], providers: [ FormBuilder] }) export class FormDoc5Component implements OnInit { validateForm: FormGroup; controlArray: Array = []; addField(e?: MouseEvent) { if (e) { e.preventDefault(); } const id = (this.controlArray.length > 0) ? this.controlArray[this.controlArray.length - 1].id + 1 : 0; const control = { id, controlInstance: `passenger${id}` }; const index = this.controlArray.push(control); // console.log(this.controlArray[this.controlArray.length - 1]); this.validateForm.addControl(this.controlArray[index - 1].controlInstance, new FormControl(null, Validators.required)); } removeField(i:any, e: MouseEvent) { e.preventDefault(); if (this.controlArray.length > 1) { const index = this.controlArray.indexOf(i); this.controlArray.splice(index, 1); console.log(this.controlArray); this.validateForm.removeControl(i.controlInstance); } } getFormControl(name:string) { return this.validateForm.controls[name]; } _submitForm() { for (const i in this.validateForm.controls) { this.validateForm.controls[i].markAsDirty(); } console.log(this.validateForm.value); } constructor(private fb: FormBuilder) { } ngOnInit() { this.validateForm = this.fb.group({}); this.addField(); } }