Search code examples
angular2-formsangular4-forms

Angular2 code on when some change in Textbox how its Reflect back to component


Here i have requirement like if i type type anything in Textbox it should be Reflect back to Component

<label> Search FRom Table:</label> <input (change)="someval()">

Someval(){
//Here i need that TextBox value
}

Solution

  • You can achieve this in Two way data binding method and so on.

    Html Looks like following

        <label> Search From Table:</label>
        <input name="searchTxt" [(ngModel)]="searchTxt" (input)="someval()">
    
        {{searchTxt}} <!-- here iam printed html file also -->
    

    Typescript File:

    //Add one variable 
    public searchTxt:any;
    
    someval(){
     console.log("Here iam printed variable",this.searchTxt);
    }
    

    Make sure you have to import Forms Module in app module file other wise it show error like this enter image description here

    app.module.ts

    import { FormsModule } from '@angular/forms'
    
    imports: [
        FormsModule,
    ],
    

    For more details about related here, please visit:

    https://www.code-sample.com/2017/05/angular2-input-textbox-change-events.html

    Angular 2 change event on every keypress