What is the digest cycle in AngularJs? Last Updated : 28 May, 2020 Comments Improve Suggest changes Like Article Like Report Before starting, we need to know a few terms related to Digest cycle. They are AngularJs watch, watch counts, and watch list. AngularJs watch: It is provided by AngularJs Framework to keep track of scope variables and the change in their values. Watches are automatically created by AngularJs Framework. It is usually done for data bindings and for which variables are decided by AngularJs Framework. Custom functions that are created for watches are called watch listeners. Watch counts: If watch count is below 2000, performance is better. We may use Angular watchers extension to count them. Angular performs watching on data-binded variables but if we want we can perform watch on normal variables as well, using watch function. It takes parameter the variable that we explicitly want to watch. Watch list: Maintains a list of all the watches associated with an angular application. i.e all the data bindings being monitored. A watch list is maintained for all scopes including root. Digest cycle Watches keep on updating new values and update DOM thus render the changes. This process is responsible for walk-through entire watches for changes.It performs dirty checking over the watches present in the watch-list. Dirty checking is to check the current values of variables from their previous values. Watch listeners are automatically executed whenever the digest process finds any modifications in the variables. It keeps note of changes and then notifies AngularJs Framework to update DOM. Thus at the end of every digest process, DOM is updated. Angular Context is a runtime environment of AngularJs Framework. First digest process performs a dirty check on watches, and checks if there are any modifications It again performs the second cycle of dirty checking on the previous cycle, watches listeners. Because there may have been variables that have got changed by others. Minimum 2 iterations are performed and the maximum 10 can run. Although it is preferred to minimize the digest cycle for better performance. Error is thrown after maximum. First level and second level updating First level updates: Suppose the variable b was updated by any event, then during the first cycle Digest cycle informs the AngularJs Framework about the changes and goes through the second cycle after that. Since there are no more updates, it thus updates DOM and completes it. Second level watch updates: Whenever there is a change encountered in the first cycle for any particular watch say c, digest process executes watch listener for it. Now watch listener further modifies variable a to a new value. After the first cycle, c gets updated. During the second cycle, we encounter changes in a and thus update takes place for a. Now a third cycle takes place and there are no more modifications encountered. DOM gets updated. An example for second level updates: html $scope.a = 1; $scope.b = 2; $scope.c = 3; $scope.$watch('a', function( newValue, oldValue ) { if( newValue != oldValue ) { console.log("a is modified to " +newValue ); } }); $scope.$watch('b', function( newValue, oldValue ) { if( newValue != oldValue ) { console.log("b is modified to " +newValue ); } }); $scope.$watch('c', function( newValue, oldValue ) { if( newValue != oldValue ) { console.log("c is modified to " +newValue ); if( $scope.c > 50 ) { $scope.a = 1000; } } }); $rootscope.$watch( function() { console.log(" digest iteration started "); }); Considering scope variables a, b, c are data binded and are eligible for the digest process. If we inspect the angular application in the browser and open the console. We can track the changes as the print statements will help us. Suppose there was a two way binding for c with an input box we could easily track the number of times it gets modified. In fact, we can inspect the digest process too, by applying $watch function on $rootscope. $watch: This function takes three parameters- watch expression, listener and object equality. Except for expression the other two are optional. The digest process starts with the root scope and later on identifies the other scopes. If our code uses DOM events (ng-click), ajax with callback, timer with callback, Browser location changes, manual invocations like $apply then it is bound to have Digest process for all of them. As we know, the browser is responsible to render the DOM and it may have events like Timer, On-click. The browser maintains a queue for these events called Event Queue. And it sends those to Javascript. Consequently, a digest takes place for them. If events are not related to Javascript i.e written in Jquery or other languages, then it is our duty to write $apply function for maintaining digest for them. $scope.$apply(function() { }); Complete scenario of Digest cycle: Comment More infoAdvertise with us Next Article What is the digest cycle in AngularJs? A ajaychawla Follow Improve Article Tags : Web Technologies AngularJS AngularJS-Misc Similar Reads What is the use of a double-click event in AngularJS ? The ng-dblclick event in the AngularJS is useful for the HTML elements for getting the double click event, defined. In case a user wishes to get the function fired or other events when the double click of the HTML elements is done, then this event will be going to be needed. All the elements of the 2 min read What is View in AngularJS ? In AngularJS, the View is what the user can see on the webpage. In an application (as per the userâs choice), the chosen view is displayed. For this purpose, the ng-view directive is used, which allows the application to render the view. Any changes made in the view section of the application are re 7 min read What are Directives in AngularJS ? AngularJS directives are extended HTML attributes having the prefix ng-. Directives are markers on the DOM element which tell Angular JS to attach a specified behavior to that DOM element or even transform the DOM element with its children. During compilation, the HTML compiler traverses the DOM mat 7 min read What is the Lifecycle of an AngularJS Controller ? The Lifecycle defines the journey of the application, which it goes through, right from creation to destruction. The lifecycle of AngularJS determines how the data interacts with AngularJS. Understanding the lifecycle enables the developer to more efficiently develop applications with greater code r 6 min read Explain AngularJS Scope Life-Cycle In AngularJS, The Scope acts as a merging parameter between the HTML and Javascript code, ie, it is the binding part between view and controller and it is a built-in object. It is available for both the view and the controller. It is used to define inside the controller, in order to define the membe 8 min read What are templates in AngularJS ? Templates in AngularJS are simply HTML files filled or enriched with AngularJS stuff like attributes and directives. A directive is a marker element that is used to target a particular attribute or class to render its behavior according to the needs. Model and controller in Angular are combined with 3 min read What are the AngularJs Global API ? Global API in AngularJS: API stands for Application Programming Interface. It is a set of protocols, routines, and tools for building software applications that allow the user to interact with the application and perform several tasks. In AngularJS Global API is a set of global Javascript functions 3 min read Two-way Data Binding in AngularJS In this article, we will see the Data Binding, along with understanding how the flow of code is from a Typescript file to an HTML file & vice-versa through their implementation. In AngularJS, Data Binding refers to the synchronization between the model and view. In Two-way data binding, the flow 3 min read Getting Started with Angular Angular is a front-end framework for building dynamic web applications. It allows the MVC (Model-View-Controller) architecture and utilizes TypeScript for building applications. So, Angular is an open-source JavaScript framework written in TypeScript. It is maintained by Google, and its primary purp 5 min read AngularJS ng-init Directive The ng-init Directive is used to initialize AngularJS Application data. It defines the initial value for an AngularJS application and assigns values to the variables. The ng-init directive defines initial values and variables for an AngularJS application. Syntax: <element ng-init = "expression" 1 min read Like