I found this thread looking for thoughts on the following: *when is it 
better to use ng-change versus a $watch on the model?*  

Right now I'm looking at code in which we *$watch* multiple properties 
within an object and call the same function when any of them changes (I 
assume this is more efficient than watching the whole object?).  Here's 
some pseudocode (I don't think a plnkr is necessary here). 

HTML:

<label><input type="checkbox" ng-model="someOptions.checkboxChoice"> 
whatever</label>
<select ng-model="someOptions.dropdownChoice" ng-options="key for (key, 
value) in dropdownChoices" />

In the directive's link function:

var updateSettings = function () {
   // do some stuff that involves the whole someOptions object
};

scope.dropdownChoices = { 'optionName1': 0, 'optionName2': 1 };

scope.someOptions = {
   checkboxChoice: true,
   dropdownChoice: 0
};

scope.$watch('someOptions.checkboxChoice', updateSettings);
scope.$watch('someOptions.dropdownChoice', updateSettings); 

...What if I just added *ng-change="updateSettings()"* to the input 
elements instead of having all the watches?  (In the actual code, we have 
more than one checkbox and dropdown, plus some radio buttons.)  It seems to 
work fine (the scope is updated by the time the call is made).  Is there a 
performance benefit to either?

Thanks,
M

-- 
You received this message because you are subscribed to the Google Groups 
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply via email to