I use this code:
$scope.$watch('message', function()
{
// the code
});
Is there any way to fire change event of message manually, so the code will be executed?
When you create a data binding from somewhere in your view to a variable on the $scope object, AngularJS creates a "watch" internally. A watch means that AngularJS watches changes in the variable on the $scope object. The framework is "watching" the variable. Watches are created using the $scope.
$apply() function will execute custom code and then it will call $scope. $digest() function forcefully to check all watch list variables and update variable values in view if any changes found for watch list variables. In most of the time angularjs will use $scope.
$digest(), $rootScope. $apply(). It updates the data binding. It iterates through all the watches and checks any value updated.
$watch(watchExpression, listener, [objectEquality]); Registers a listener callback to be executed whenever the watchExpression changes. The watchExpression is called on every call to $digest() and should return the value that will be watched.
Few options:
Use $scope.$apply() to run the digest loop which call all of the watch expressions
Put you inner watch code inside a function and call it manually
Change messages
:)
Another option here is declaring the function separately and using $scope.watch
with the pointer.
var watchFunction = function(){
// the code
}
$scope.$watch('message',watchFunction);
watchFunction();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With