Honestly, no wonder they write that this is a difficult topic to understand. I did not understand, like most.
Where to use this thing?

Example:

.directive('clickable', function() { return { restrict: "E", link: function($scope, element, attrs) { element.bind('click', function() { $scope.$apply(function() { $scope.user++; $scope.bar++; }); console.log('1') }); } } }); 

Without the $scope.$apply() wrapper, the example will not work.

I do not know, I found this note:

Important: any browser events are raised outside of the AngularJS scope, so inside your event handlers you need to call $scope.$apply .

1 answer 1

So, what exactly is not clear? The $ apply / $ digest functions start dirty-check, and if the data in $ scope has changed, then update the view, that's all magic. When you change the data inside angular, for example, to ng-click, it calls the $ apply method, if your code is not executed through angular, you need to run dirty-check with your hands. Here are some very rough examples:

ng-click

 <button ng-click="counter++">counter</button> 

within itself angular, replaced this construction with:

 jqLite('button').on('click', function (evt) { evt.preventDefault(); $scope.counter++; $scope.$apply(); }); 

or

 $timeout(function () { $scope.foo = "bar"; }, 100); // эквивалентна setTimeout(function () { $scope.foo = "bar"; $scope.$apply(); }, 100); 

All this is very approximate, but the bottom line is that when you use binding or angular methods, it calls $ apply / $ digest for you, that's all, there’s nothing complicated about it.

  • Thanks ... can I ask for a couple more examples? - koza4ok
  • Yes there is no where to spit, at the end will be $scope.$apply() :] - RubaXa
  • OK ... Thank you. I'll shove if there is no update. - koza4ok