$watch service helps
us to do something or run any business logic when any property or variable
value attached to $scope changed
$watch(variable,
listener)
function MyController($scope) {
$scope.myVar = 1;
$scope.$watch('myVar', function() {
alert('hey, myVar has changed!');
});
$scope.buttonClicked = function() {
$scope.myVar = 2; // This will
trigger $watch expression to kick in
};
}
There are some other additional angular service $watchCollection and $watchgroup .
Scope.$ watchgroup:
it helps us to watch a set of group of variables together , if anyone of them
is changed, it will trigger $watchGroup expression.
$watchGroup(watchExpression,
listener)
Scope.$watchCollection
: it helps us to watch whole object’s properties , we need to pass object
instead of each properties of object.
$watchGroup(watchobject,
listener)
Other related links :
AngularJs :$broadcast () , $on() and $emit()
Other related links :
AngularJs :$broadcast () , $on() and $emit()
No comments:
Post a Comment