Open In App

Angular forms NgModel Directive

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

Syntax:



<Input [(NgModel)= 'name']>

NgModule: Module used by NgModel is:

Selectors:



Approach: 

Example 1:




import { Component, Inject } from '@angular/core';
  import { PLATFORM_ID } from '@angular/core';
  import { isPlatformWorkerApp } from '@angular/common';
  
  @Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styleUrls: [ './app.component.css' ]
  })
  export class AppComponent  {
    gfg: string = '';
  
  setValue() {
    this.gfg = 'GeeksforGeeks';
  }
  }





<h1>GeeksforGeeks</h1>
  
<input [(ngModel)]="gfg" #ctrl="ngModel" required>
  
  
<p>Value: {{ gfg }}</p>
  
  
  
<button (click)="setValue()">Set value</button>

Output:

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


Article Tags :