Open In App

Angular forms NgModelGroup Directive

In this article, we are going to see what is NgModelGroup in Angular 10 and how to use it. The NgModelGroup is used to create a top-level form group Instance, and it binds the form to the given form value.

Syntax:



<div ngModelGroup="name"></div>

NgModule: Module used by NgModelGroup is:

Selectors:



 

Approach: 

Example:




import { Component, Inject } from '@angular/core';
  
  @Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styleUrls: [ './app.component.css' ]
  })
  export class AppComponent  {
    gfg:any;
  
  setValue() {
    this.gfg = {first: 'Geeky', email: 'abc@gfg.com'};
  }
}




<br>
<form #f="ngForm">
  <div ngModelGroup="gfg" #nameCtrl="ngModelGroup">
    Name: <input name="first" [ngModel]="gfg.first" >
    Email: <input name="email" [ngModel]="gfg.email" >
  </div
</form>
<br><br>
  
<button (click)="setValue()">Set value</button>

Output:

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


Article Tags :