Closed timothylombrana closed 8 years ago
Gimme some context :P
`$scope.$watch('expressionOne', function(newVal){ console.log(newVal); }); $scope.$watch('expressionTwo', function(newVal){ console.log(newVal); }); $scope.$watch('expressionThree', function(newVal){ console.log(newVal); });
//As oppsed to: //All unrelated variables but being all watched under same function, in same JS file. var collection = ['expressionOne', 'expressionTwo', 'expressionThree']; $scope.$watchCollection('collection', function(newCollection, oldCollection, scope) { // some code here for given expression. });`
I see, try out $scope.$watchGroup
:
https://docs.angularjs.org/api/ng/type/$rootScope.Scope#$watchGroup
Id like to test if it be more performant use $watchGroup, instead of having several $watch functions. What do you think?
Well, ideally you shouldn't really need $watches at all, they're usually never needed. If you absolutely need them then I'd use group over collection :)
Then thats what I'll do, not need watchers! Thanks again!
What I mean is to have several $scope.$watch functions running in specific view or is it better to use $scope.$watchCollection even if the variables are unrelated? So as to have only one watch function in that file.