$scope.$apply(), $scope.$digest()

$scope.$apply(), $scope.$digest() http://jimhoskins.com/2012/12/17/angularjs-and-apply.html

by william jiang

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.0-beta.1/angular.min.js"></script>
<div ng-app="jsfiddle">
    <div ng-controller="Ctrl1">{{message}}</div>
    <div ng-controller="Ctrl2">{{message}}</div>
</div>
<script>
    angular.module('jsfiddle', [])
        .controller('Ctrl1', Ctrl1)
        .controller('Ctrl2', Ctrl2);

    function Ctrl1($scope) {
        $scope.message = "Waiting 2000ms for update";

        setTimeout(function() {
            $scope.message = "Timeout called!";
            // AngularJS unaware of update to $scope
        }, 2000);
    }

    function Ctrl2($scope) {
        $scope.message = "Waiting 2000ms for update";

        setTimeout(function() {
            //$scope.$apply(function() {
            $scope.message = "Timeout called!";
            $scope.$digest();
            //});
        }, 2000);
    }
</script>