Open In App

Angular forms FormGroupDirective

In this article, we are going to see what is FormGroupDirective in Angular 10 and how to use it.

FormGroupDirective is used to Bind an existing FormGroup to a DOM element.



Syntax:

<form [FormGroup] ="name">

Exported from:



 

Selectors:

Approach: 

Example 1:




import { Component, Inject } from '@angular/core';
  import { FormGroup, FormControl, FormArray } from '@angular/forms'
  @Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styleUrls: [ './app.component.css' ]
  })
  export class AppComponent  {
    form = new FormGroup({
      name: new FormControl()
    });
  
    get name(): any {
      return this.form.get('name');
    }
    
    onSubmit(): void {
      console.log(this.form.value); 
    }
    
  }




<br>
<form [formGroup]="form" (ngSubmit)="onSubmit()">
  <input formControlName="name" placeholder="Name">
  <br>
  <button type='submit'>Submit</button>
  <br>
  <br>
</form>

Output:

Reference: https://angular.io/api/forms/FormGroupDirective


Article Tags :