wondering if there is a way in angular to watch a function like you would a variable
function foo() {
console.log("foo called");
}
$scope.$watch('foo', function {
console.log("foo watched");
});
So given the syntax is correct, will "foo watched" be printed to the console, if not, is there a way to have that watch function run?
EDIT
Reformatting question to more of what I need:
Let's say I have a button and an text input, each handled by different controllers, say btnCtrl
and txtCtrl
There is also a boolean global flag stored as $scope.flag
which changes the validation standards for the input.
//btnCtrl
//called on button click
function onButtonClick() {
$scope.flag = !$scope.flag
}
//txtCtrl
//called on input change
function onInputChange() {
handleValidations()
}
function handleValidations() {
//set error indicator on invalid input
//valid input changes based on $scope.flag value
if($scope.flag) {
//check validation1
} else {
//check validation2
}
}
I want to check the input box validations, handleValidations
when $scope.flag
is changed. $scope.flag
is changed frequently not by the button, but by other functions.
So, I would like to call handleValidations
not when $scope.flag
is changed, but when onButtonClick
is called. What is the best way to do that?
My initial thought was to put some sort of $scope.$watch
on onButtonClick
.