by Sai gowtham

Two-way data binding in Angular using ngModel directive

In this tutorial, we are going to learn about angular two-way data binding using the ngModel directive.

What is Two-way data binding?

Two-way data binding means data flows in both directions, The data in view changes it also updates the model, The data in the model changes it also updates the view.

ngModel directive

Angular provides us a ngModel directive by using that we can sync the data in both directions.

Example

To use a ngModel directive inside our components first we need to import the FormsModule in app.module.ts file and add it to the imports array.

app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { AppComponent } from './app.component';

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

Let’s use the ngModel model directive.

app.component.ts
import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})

export class AppComponent {
  myname = 'Angular';}
app.component.html
<div>
    <input [(ngModel)]="myname" />    <h1>{{myname}}</h1>
</div>

In the above code ,we have added [(ngModel)]= "myname" it means we are binding input element in both directions to myname property.

angular-two-databinding-example

Have you seen in the above image, two-way data binding is working successfully.

Css Tutorials & Demos

How rotate an image continuously in CSS

In this demo, we are going to learn about how to rotate an image continuously using the css animations.

How to create a Instagram login Page

In this demo, i will show you how to create a instagram login page using html and css.

How to create a pulse animation in CSS

In this demo, i will show you how to create a pulse animation using css.

Creating a snowfall animation using css and JavaScript

In this demo, i will show you how to create a snow fall animation using css and JavaScript.

Top Udemy Courses

JavaScript - The Complete Guide 2021 (Beginner + Advanced)
JavaScript - The Complete Guide 2021 (Beginner + Advanced)
64,026 students enrolled
52 hours of video content
View Course
React - The Complete Guide (incl Hooks, React Router, Redux)
React - The Complete Guide (incl Hooks, React Router, Redux)
284,472 students enrolled
40 hours of video content
View Course
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
152,857 students enrolled
48.5 hours of video content
View Course