4

I Have a template-driven form with two-way data binding and validator

<input [(ngModel)]="myObj.name" someValidator>

How can I prevent sendind data from input do model if there are some validation errors? In my model I want to have only correct data from form.

Jarosław Rewers
  • 1,059
  • 3
  • 14
  • 23
  • 2
    Possible duplicate of [two way binding with elvis-operator](http://stackoverflow.com/questions/36016407/two-way-binding-with-elvis-operator) – eko Apr 27 '17 at 07:46

1 Answers1

1

You can attach a keyup function to the input and do whatever you wish to achieve.

<input [ngModel]="myObj.name" (keyup)="onChangeCheck($event.target.value)"> 

 export class ParentCmp {

   onChangeCheck(val){
      console.log(val);
        if( condition){
            this.myobj.name = value;
         }else{
             // do nothing
         }
    }
}
nircraft
  • 8,242
  • 5
  • 30
  • 46