Angular Add Form Fields Dynamically Example

If you need to see example of dynamically add and remove form input fields with angular. step by step explain how to dynamically add and remove form fields in angular. This tutorial will give you simple example of how to add remove input fields dynamically angular. i explained simply step by step adding form fields dynamically in angular. Here, Creating a basic example of dynamically add and remove form input fields with angular.

I will show you very simple and step by step example of add more input fields dynamic in angular 6, anuglar 7, angular 8 and angular 9 application.

In this example we will create form with product name and user can add multiple quantity with price. we will use formgroup and formarray to create dynamic form in angular application.

You can see bellow layout for demo. let's follow bellow step.

Import FormsModule:


import { NgModule } from '@angular/core';

import { BrowserModule } from '@angular/platform-browser';

import { FormsModule, ReactiveFormsModule } from '@angular/forms';

import { AppComponent } from './app.component';


imports: [ BrowserModule, FormsModule, ReactiveFormsModule ],

declarations: [ AppComponent ],

bootstrap: [ AppComponent ]


export class AppModule { }

updated Ts File

In this file we will first import FormGroup, FormControl,FormArray and FormBuilder from angular forms library.


import { Component } from '@angular/core';

import { FormGroup, FormControl, FormArray, FormBuilder } from '@angular/forms'


selector: 'my-app',

templateUrl: './app.component.html',

styleUrls: [ './app.component.css' ]


export class AppComponent {

name = 'Angular';

productForm: FormGroup;

constructor(private fb:FormBuilder) {

this.productForm = this.fb.group({

name: '',

quantities: this.fb.array([]) ,



quantities() : FormArray {

return this.productForm.get("quantities") as FormArray


newQuantity(): FormGroup {

return this.fb.group({

qty: '',

price: '',



addQuantity() {



removeQuantity(i:number) {



onSubmit() {




Template Code:

In this step, we will write code of html form with ngModel. so add following code to app.component.html file.

I used bootstrap class on this form. if you want to add than then follow this link too: Install Boorstrap 4 to Angular 9.


<div class="container">

<h1>Angular FormArray Example - HDTuto.com</h1>

<form [formGroup]="productForm" (ngSubmit)="onSubmit()">


<label for="name">Product Name:</label>

<input type="text" id="name" name="name" formControlName="name" class="form-control">


<table class="table table-bordered" formArrayName="quantities">


<th colspan="2">Add Multiple Quantity:</th>

<th width="150px"><button type="button" (click)="addQuantity()" class="btn btn-primary">Add More</button></th>


<tr *ngFor="let quantity of quantities().controls; let i=index" [formGroupName]="i">


Quantity :

<input type="text" formControlName="qty" class="form-control">




<input type="text" formControlName="price" class="form-control">



<button (click)="removeQuantity(i)" class="btn btn-danger">Remove</button>




<button type="submit" class="btn btn-success">Submit</button>



{{this.productForm.value | json}}


Now you can run your application using following command:

ng serve

I hope it can help you...

April 29, 2020 | Category : Angular

Blog Search