We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy.
Unfortunately, activation email could not send to your email. Please try again.

Angular Reactive Forms

Angular reactive forms facilitate a reactive style of programming that favors explicit management of the data flowing between a non-UI data model (typically retrieved from a server) and a UI-oriented form model that retains the states and values of the HTML controls on screen. Reactive forms offer the ease of using reactive patterns, testing, and validation.

With reactive forms, you create a tree of Angular form control objects in the component class and bind them to native form control elements in the component template, using various techniques.

Syncfusion Angular Components supports reactive forms. To use Reactive Forms in our application, we must import ReactiveFormsModule in application’s root module.

Reactive Form API’s

FormControl are the basic building blocks of a reactive form or a form in general.

FormGroup is a collection/group of FormControls. It also offers many api’s such as tracking validity and values of the whole formGroup.

Refer to the below code snippet for view file.

<form class="form-horizontal" [formGroup]="form">

 

  <div class="form-group">

    <div class="col-sm-12">

      <label for="name" class="control-label required">Name</label>

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

    </div>

  </div>

 

  <div class="form-group" >

    <div class="col-sm-12">

      <label for="email" class="control-label required">Email</label>

      <input type="text" class="e-textbox" id="email" class="form-control" formControlName="email">

    </div>

  </div>

  <div class="form-group" >

    <div class="col-sm-12">

      <label for="password" class="control-label required">Password</label>

      <input type="text" class="e-textbox" id="password" class="form-control" formControlName="password">

    </div>

  </div>

 

  <button class="btn btn-primary" ej-button (ejclick)="onSubmit()" text="Submit" [disabled]="!form.valid"></button>

  <button class="btn" ej-button (ejclick)="reset()" text="Reset"></button>

 

</form>

 

 

Refer to the below code snippet for model file of reactive form. In this form, the submit button will work only if the valid details will be given by the user.

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

import { FormGroup, FormBuilder, Validators } from '@angular/forms';

 

@Component({

  selector: 'reactive-form',

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

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

})

export class ReactiveFormComponent implements OnInit {

    form: FormGroup;

    private formSumitAttempt: boolean;

    

    constructor(private formBuilder: FormBuilder) {}

  

    ngOnInit() {

      this.form = this.formBuilder.group({

        name: [null, Validators.required],

        email: [null, [Validators.required, Validators.email]],

        password: [null,Validators.required]

      });

    }

  

    onSubmit() {

      this.formSumitAttempt = true;

      if (this.form.valid) {

        console.log('form submitted');

        console.log(this.form.value)

      }

    }

  

    reset() {

      this.form.reset();

      this.formSumitAttempt = false;

    }

  }

 

Note: We integrated the Reactive forms in our angular seed. Refer the seed here.

 

Article ID: Published Date: Last Revised Date: Platform: Control:
8037 10/13/2017 10/13/2017 JavaScript General
Did you find this information helpful?
Add Comment
You must log in to leave a comment

You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.