Friday, 17 June 2016

javascript - When should $scope.apply() be used in angular?

What does $scope.apply do? In some cases, if $scope.apply() is not present, $scope.watch() is not triggered. For example,



The controller has the following:




setTimeout(function(){
$scope.person = someperson;
}, 500);


person is being watched in the directive.



$scope.watch('person', function(){
console.log($scope.person);
$scope.apply();

});


In this case, watch is triggered only when apply is there.

No comments:

Post a Comment