Draex_
Draex_

Reputation: 3474

Firing $watch event manually in angularjs

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?

Upvotes: 27

Views: 29851

Answers (2)

robstarbuck
robstarbuck

Reputation: 8091

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();

Upvotes: 12

Shai Reznik - HiRez.io
Shai Reznik - HiRez.io

Reputation: 8948

Few options:

  1. Use $scope.$apply() to run the digest loop which call all of the watch expressions

  2. Put you inner watch code inside a function and call it manually

  3. Change messages :)

Upvotes: 42

Related Questions