Deep watching on large objects a performance killer—so I'd like to be able to pass a dynamic object/property into the watchExpression parameter of the $scope.$watch function, whenever you invoke an action that changes that particular object or property.
I also have a bunch of different objects and don't want to set up watches for all of them.
For example:
var watchingFunction = function (objectToBeWatched) {
$scope.$watch(function (objectToBeWatched) {return objectToBeWatched;}, function(newValue, oldValue) {
if (newValue !== oldValue) {...};
}, true);
$scope.object = true;
$scope.changeMyObject = watchingFunction(object);
HTML
<input type="checkbox" ng-model="object" ng-change="changeMyObject()">
I believe this wouldn't work because invoking the function watchingFunction() only runs $scope.$watch() once? Where as if you define $scope.$watch on the $scope (instead of wrapped inside the watchingFUnction() it is continually watching $scope.$watch's watchExpression?
If this is the case, are there any creative things you can do to return a value from watchExpression which takes an outside parameter instead of explicitly declaring the object within $scope.$watch's watchExpression?
Thanks a bunch!
EDIT Additional clarity—I'm using the $scope.$watch to make $scope.$broadcast's to another scope every time a value on the current scope changes.
ng-change), then you don't need a $watch. What are you actually trying to achieve? What is the use case?$scope.$watch?