Angularjs Examples

AJS Examples


AngularJS $scope.$digest() Function

The AngularJS $digest method is used to start digest life cycle on $scope and children. The $digest method evaluates the watch expressions of the watchers of $scope and its children and if necessary executes the watch actions until no more listeners are firing. AngularJS (if used properly) will handle the digest lifecycle, so don't have to call $digest method manually.

This method will throw 'Maximum iteration limit exceeded.' if the number of iterations exceeds 10.

Syntax

$scope.$digest(); 

AngularJS $scope.$digest() Function Example


<!DOCTYPE html>        
<html>        
<head> <!-- www.techstrikers.com -->        
 <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.0-beta.1/angular.min.js"></script>         
    <meta charset="utf-8">        
    <title>AngularJS $digest Example</title>        
  <script>        
var app = angular.module('app', []);        
    app.controller("DigestController", function($scope) { 
      
      $scope.name = 'techstrikers';
      $scope.counter = 0;

      $scope.$watch('name', function(newValue, oldValue) {
        $scope.counter = $scope.counter + 1;
      });
      //expect(scope.counter).toEqual(0);
      $scope.Change = function() {
        $scope.$digest();
      }
    });     
</script>        
</head>        
<body style="background-color:#DDE4E9;">        
  <fieldset style="background-color:#DDE4E9;">                    
    <legend>AngulerJS $digest Method Example</legend>         
  <div ng-app="app">              
    <div ng-controller="DigestController">   
      <p style="font-family:Arial;color:yellow;background:steelblue;padding:3px;width:350px;"    
     >{{counter}}</p>
     <input ng-model="name">  
    <button ng-click="childCall()">$digest</button>  
    </div>              
</div>        
 </fieldset>         
</body>        
</html>