How to attach input filed to another in Angular 2 only if it's pristine?

Issue

I have two input fields:

<input type="text" [(ngModel)]="title" name="title">
<input type="text" [(ngModel)]="og_title" name="og_title" value="{{ title }}">

I want to make the og_title field to equal to title until og_title is pristine.

How can I do this in Angular 2 (Angular 7)

Solution

I think it would be better if you use Reactive forms instead of two way binding with ngModel, but still this would work :

<input type="text" [(ngModel)]="title" name="title" (input)="titleChanged()">
<input type="text" [(ngModel)]="og_title" name="og_title">

and in the TS file :

titleChanged(){
 if(<code to check if og_title is pristine>){
  this.og_title = this.title;
 }
}

replace the <code to check if og_title is pristine> with logic that includes name of the form and pristine condition of input field

Answered By – Pavan Skipo

Answer Checked By – Willingham (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.