In order to understand what $scope.$apply() gives us and why we would need it  is necessary to take a step back and look at they way AngularJS checks if there has been any changes to its model or its views so that it can update its bindings (and the whole process that goes behind it).

Digest cycle

In AngularJS there is a concept of digest cycle, which is pretty much a loop in which AngularJS checks for any changes to its bindings plus anything else that is being watched and lives inside its $scope (note that not everything that lives inside the $scope is being watched by Angular, only variables used in expressions such as  <div>{{message}}</div>and also when $scope.$watch is being used.)

Once a watch is fired Angular starts a new cycle ( $digest cycle) in which it checks all current variables and compare them with their previous values, if it finds any change it will then update the correspondent bindings,  if it doesn’t it will then wait until the next watch fires.

Why do I need $scope.$apply() then?

Angular only monitors variables used in expressions and anything inside of a $watch living inside the $scope. So if you are changing the model outside of the Angular context you will need then to call $scope.$apply() for those changes to be propagated, otherwise Angular will not know that they have been changed thus the binding will not be updated.

Let’s look at the below example were I’m changing the model from outside the $scope therefore any changes that I make are not visible

And this is the code in my view:

The above JavaScript code will update the model two seconds after the page has been loaded, but as this is not being watched by AngularJS (the $scope.message is but not the timeout function) no changes will be visible on the screen

So if we want the changes to be propagated we’ll need to call the $scope.$apply():


If you are changing your model outside of your AngularJS context (or the variable that you want isn’t being watched) then you will need to use $scope.$apply in AngularJS as a way of telling it that it will need to fire a new $digest cycle so that it can detect your changes.