$scope.apply() will trigger the $digest loop of AngularJS. To put is simple, it's just a convenient way to trigger the app rerender.
Usually it is used when you want to run a piece of code that is outside of angular app.
Direct from the documentation:
$apply() is used to execute an expression in angular from outside of
the angular framework. (For example from browser DOM events,
setTimeout, XHR or third party libraries). Because we are calling into
the angular framework we need to perform proper scope life cycle of
exception handling, executing watches.
Example of using scope.$apply() with jQuery Datepicker:
angular.module('customApp', []).directive('datepicker', function () {
return {
require: 'ngModel',
link: function (scope, element, attrs, ngModelCtrl) {
jQuery(element).datepicker({
onSelect: function (date) {
scope.myDate = date;
scope.$apply();
}
});
}
};
});