question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

keypress and click events should be triggered after ng-model changes the value

See original GitHub issue

THIS IS A FEATURE REQUEST. I’m currently using Angular 2.2.1

I’m trying to respond to an input field change event like this…

<input type="text" [(ngModel)]="user.name" (keypress)="doSomething()">

The only problem with “(keypress)” is that “user.name” will not yet contain the most recent value when the doSomething() method is fired because events such as ‘keypress’ and ‘click’ are triggered before ng-model changes the value. Therefore, it has been recommended to use “(ngModelChange)” instead.

<input type="text" [(ngModel)]="user.name" (ngModelChange)="doSomething()">

But the dilemma with “(ngModelChange)” is that the method doSomething() will get triggered not only when the user is typing in the input field, but also whenever any changes are made to the model programmatically.

WANTED: Something like “(keypress)” where the event is triggered after ng-model changes the value (I’ve yet to understand why this shouldn’t be the default behavior) OR alternatively, a way to know within “(ngModelChange)” whether the event was triggered in response to a user interaction with the input field as opposed to a programmatic change to the model, as a workaround.

See similar closed issue: “click event triggers before ng-model changes the value.” #3406

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

6reactions
stenflocommented, Nov 24, 2016

RESOLVED!!!

My solution was to use (input) instead of (keypress) or (ngModelChange).

My pitfall was that I didn’t know about (input), and I don’t see it mentioned in the docs.

I’m marking this issue as closed. Thanks for all the help on this one!!!

1reaction
zoechicommented, Nov 15, 2018

@irrfan13 sounds like you are looking for keydown. You can listen to any DOM event using (event)="..." binding. input also is just the DOM event the browser provides. ngModelChange is an Angular event as ng... indicates.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Angular 2 change event on every keypress - Stack Overflow
I just used the event input and it worked fine as follows: in .html file : <input type="text" class="form-control" ...
Read more >
What is the difference between change and ngModelChange ...
The change event is not necessarily fired for each alteration to an element's value. change is a DOM event that is it can...
Read more >
NgModelChange & Change Event in Angular - TekTutorialsHub
NgModelChange is an Angular specific event, which we can use to listen for changes to the user input. , We can also use...
Read more >
ngModelChange and change events in Angular with examples
In Angular, We will use ngModel for two way data binding. Whenever a change happens in ngModel , Angular will trigger ngModelChange event....
Read more >
ngChange - AngularJS: API
Evaluate the given expression when the user changes the input. The expression is evaluated immediately, unlike the JavaScript onchange event which only triggers...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found