Two way data binding in angular2

First we have to include form module in app.module.ts

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

@NgModule({
  imports:      [ BrowserModule,FormsModule ],
  declarations: [ AppComponent ],
  bootstrap:    [ AppComponent ]
})
export class AppModule { }
 
//app.componene.ts
import { Component } from '@angular/core';

@Component({
  selector: 'my-app',
  template: `<h1>My First Angular 2 App</h1>
    <input [(ngModel)]="firstName">
{{firstName}}
    
  `
})
export class AppComponent { }

Leave a Reply

Your email address will not be published. Required fields are marked *