Reputation: 4834
When invoking method from outside AngularJs I need to invoke $digest/$apply, but I don't want to take care of that. Is there a good way to do that?
<button ng-click='sayHello()'>greet</button>
{{greeting}}
//This works fine!
$scope.sayHello = function () {
$scope.greeting = 'Hello!';
};
//This needs a $digest!
$scope.sayHello = function () {
//setTimeout simulates invokation from outside AngularJs.
setTimeout(function () {
$scope.greeting = 'Hello!';
//I don't want to think about $digest();
//So, how to do this without the $digest?
$scope.$digest(); //or $scope.$apply();
}, 1000);
};
Upvotes: 0
Views: 70
Reputation: 1434
For your specific case of setTimeout you would want to use the $timeout built-in. In other cases (eg. JQuery Events) you can wrap your Code in an apply block thus lifting it into the AngularJS digest cycle like so:
setTimeout(function () {
$scope.$apply(function(){
//Your Code goes here
}
}, 1000);
Upvotes: 2