I'm in a directive, and I would like to do a union $watch
on a variable that exists on the local $scope
and one that exists on $rootScope
. It would look something like:
myapp.directive('cohnbread',
['TEMPLATE_URL', '$rootScope',
function(TEMPLATE_URL, $rootScope) {
return {
templateUrl: TEMPLATE_URL + "cohnbread.html",
scope: { odp: '@' },
link: function(scope, elem, attrs) {
// here I would like to register a callback to fire
// whenever scope.bar or $rootScope.foo fires. Something like:
$watchBoth('scope.bar', '$rootScope.foo', onBarOrFooChange);
}
};
}]);
Is there a workaround to make this happen?
Edited to provide more info.
If you're just wanting to fire a callback when one or the other changes, just setup a $watch
for each one. For example:
scope.$watch('bar', onBarOrFooChange);
scope.$watch('foo', onBarOrFooChange);
or if your directive has an isolated scope, you could setup the $watch
for foo
like this:
scope.$watch(function() {return $rootScope.foo}, onBarOrFooChange);