angularjs select: watch the ng-model versus use ng-change

Been wondering about the best approach when using angulars select. You pass it an ng-model to hold the currently selected choice (including initialization).

Angular provides at least 2 options for being notified of changes in the dropdown:
1. use ng-change on the select and have it call a method on scope to handle the change
2. watch the ng-model and when it changes then handle the change

So which is better?

A typical mvc approach is to listen to the model and respond to changes. Angular allows us to watch the model and do same. For watch read listen.
One downside of watching the selects ngModel is that watch fires a lot. You do get new and prev values passed in the watch so you could compare both and do nothing if same and thus save some cycles.
Using ng-change is simpler, it only fires when the drop down changes. You can see whats happening by looking at the html (although it should not be so difficult to understand by looking at the watch code either)

I prefer #2 watch the model. I will try and see how well it works in my code and report back findings..

Comments

Popular posts from this blog

deep dive into Material UI TextField built by mui

angular js protractor e2e cheatsheet

react-router v6.4+ loaders, actions, forms and more