34

Is there a way to get the previous(last) value of a field on ngModelChange? What I have goes something like this

HTML

<input type="text" [(ngModel)]="text" (ngModelChange)="textChanged($event)">

Handler

private textChanged(event) {
    console.log('changed', this.text, event);
}

What I get is

changed *newvalue* *newvalue*

Of course I can keep the older value using another variable, but is there a better way?

Rohan Fating
  • 2,045
  • 12
  • 23
irtaza
  • 671
  • 1
  • 6
  • 13

4 Answers4

39

What you can do is,

DEMO : http://plnkr.co/edit/RXJ4D0YJrgebzYcEiaSR?p=preview

<input type="text" 
       [ngModel]="text"                      //<<<###changed [(ngModel)]="text" to [ngModel]="text"
       (ngModelChange)="textChanged($event)"> 

private textChanged(event) {        
    console.log('changed', this.text, event);
    this.text=event;                          //<<<###added 
}
micronyks
  • 49,594
  • 15
  • 97
  • 129
30

So found kinda weird(at least for me) possible solution for this with least changes in the code in question. So on assigning the (ngModelChange) attribute before [(ngModel)] what I get is following with the same handler:

changed *older value* *new value*

I get the new value in this.textlike so:

setTimeout(() => console.log(this.text), 0);
irtaza
  • 671
  • 1
  • 6
  • 13
11

all you need to do is to put (ngModelChange)="textChanged($event)" to the left of [(ngModel)] element in the html tag, like:

<input (whatever...) (ngModelChange)="textChanged($event)" [(ngModel)]="text">

This way, inside textChanged(event), the element you are binding to still has the previous value, while event is the new one

If you type

<input (whatever...) [(ngModel)]="text" (ngModelChange)="textChanged($event)">

The event will be called with new value only. However:

<input (whatever...) (ngModelChange)="textChanged($event)" [(ngModel)]="text" (ngModelChange)="textChanged($event)">   - 

Will get you both events with previous and current value

DiSaSteR
  • 488
  • 5
  • 9
  • 1
    This is the correct answer. I did not know that the order of the (ngModelChange) and the [(ngModel)] inside the element is important. Thank you – Semir Deljić Feb 28 '19 at 15:52
  • Yes this is the correct workaround for the Angular bug!! In the Angular Doco, under ngModel it says: "Event emitter for producing the ngModelChange event after the view model updates." NOTE the "after the view model updates" So that would make it a Angular bug, because it is NOT deterministic!! – GregJF Jul 15 '20 at 01:59
  • If you need both on and new values then don't use the (ngModelChange)="eventHandlerMethod($event)", use the (change)="eventHandlerMethod($event)" – GregJF Jul 15 '20 at 02:05
2
<input (ngModelChange)="preTextChanged($event)" [(ngModel)]="text" (ngModelChange)="postTestChanged($event)">

In this way you can know the previous value and the next value

Dani Andújar
  • 827
  • 1
  • 10
  • 12
  • 1
    This behavior was so strange to me. Why would the attribute position for a tag make any difference? They should be named separate. Thank you for clearing this for me. – Jimmy Garpehäll Feb 17 '20 at 13:32