Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks

How to make a reactive form in angular?

Aman Jain, August 29, 2021September 10, 2021

Angular has vast library of modules so reactive form is one of the part of angular library.

Prerequisites knowledge:

  1. Angular
  2. HTML
  3. CSS
  4. Javascript

So, let’s start building a from .

Steps to create a reactive form in angular

  1. Create a new or existing angular project, you can also read this article to create new angular project.
  2. Create a component with ng generate command.

    ng generate component helloworld

    ng generate component
  3. Import ReactiveFormsModule module add it in to import block.
import { NgModule } from '@angular/core';
import { ReactiveFormsModule } from '@angular/forms';
import { BrowserModule } from '@angular/platform-browser';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { HelloworldComponent } from './helloworld/helloworld.component';

@NgModule({
  declarations: [
    AppComponent,
    HelloworldComponent
  ],
  imports: [
    BrowserModule,
    ReactiveFormsModule,
    AppRoutingModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

4. Edit helloworld.component.ts and import FormGroup and FromControl from @angular/form

import { Component, OnInit } from '@angular/core';
import {  FormControl, FormGroup, Validators } from '@angular/forms';

@Component({
  selector: 'app-helloworld',
  templateUrl: './helloworld.component.html',
  styleUrls: ['./helloworld.component.css']
})
export class HelloworldComponent implements OnInit {

  form: FormGroup = new FormGroup(
      {
        name:new FormControl("inital value ",[Validators.required]),
        email:new FormControl("inital value ",[Validators.required]),
        password:new FormControl("",[Validators.required]),
        
      }
    ); // creating a form group
  constructor() { }

  ngOnInit(): void {

     
  }

  submitForm(){ //handle the form after submit

    console.log(this.form.value);

  }

}

5. Edit HTML file, add attribute [formGroup] to form and formControlName to each input

<h1>Helloworld works!</h1>
<form [formGroup]="form" (submit)="submitForm()">
<div>
    Name : <input type="name" formControlName="name" placeholder="Name"  /> <br>
</div>
<div>
    Email : <input type="email" formControlName="email"  placeholder="Email" /> <br>
</div>
<div>
    Password : <input type="password" formControlName="password"  placeholder="Password" /> <br>
</div>
<div>
    <button type="submit">Submit</button>
</div>

</form>


Output :

Screenshot 2021 08 29 at 10.25.43 PM
Reactive form with submit
You can also read about  How to upload multiple files and form in angular 12 reactive form

Related

Javascript Angular

Post navigation

Previous post
Next post

Related Posts

What is Access Modifiers ?

July 18, 2021August 2, 2021

In object oriented programming(oops) to set the visibility of classes, variables and methods we are using access modifiers. So Access modifiers are used the encapsulation of class properties. There are three types of access modifiers in any language (Php, java or c etc. ) Private Protected Public 1. Private Access…

Read More
Javascript How to copy object without reference in angular

How to copy object without reference in angular ?

April 13, 2022April 14, 2022

In this tutorial we will learn to copy object without reference in angular. Objects and array are work on reference by address methodology means Objects and array keeps the address means if you change anything in the object or array from anywhere then it will change the value in original…

Read More
Uncategorized setup simple jQuery form validation

How to setup simple jQuery form validation?

September 15, 2021September 29, 2021

In this tutorial, we are going to learn jQuery form validation using jQuery validation plugin. this plugin have many features like to validate text, email, phone number , sync Ajax request etc. Let’s begin with step by step guide: Step 1: Create a html file Firstly we are going to…

Read More

Aman Jain
Aman Jain

With years of hands-on experience in the realm of web and mobile development, they have honed their skills in various technologies, including Laravel, PHP CodeIgniter, mobile app development, web app development, Flutter, React, JavaScript, Angular, Devops and so much more. Their proficiency extends to building robust REST APIs, AWS Code scaling, and optimization, ensuring that your applications run seamlessly on the cloud.

Categories

  • Angular
  • CSS
  • Dart
  • Devops
  • Flutter
  • HTML
  • Javascript
  • jQuery
  • Laravel
  • Laravel 10
  • Laravel 11
  • Laravel 9
  • Mysql
  • Php
  • Softwares
  • Ubuntu
  • Uncategorized

Archives

  • May 2025
  • April 2025
  • October 2024
  • July 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • July 2023
  • March 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Recent Posts

  • Understanding High Vulnerabilities: A Deep Dive into Recent Security Concerns
  • Understanding High Vulnerabilities in Software: A Week of Insights
  • Blocking Spam Requests with LaraGuard IP: A Comprehensive Guide
  • Enhancing API Development with Laravel API Kit
  • Exploring the Future of Web Development: Insights from Milana Cap
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version