Angularjs in $watch (), $digest () and $apply ()

Source: Internet
Author: User

AngularJS $scope inside of $watch (), $digest () and $apply () are the core functions of AngularJS, and learning angularjs must understand these functions.

When binding variables in $scope to view, Angularjs automatically creates a "Watch" inside. "Watch" is used to monitor changes in variables in the ANGULARJS scope. You can create "watch" by calling the $scope. $watch () method.

$scope. The $digest () function iterates through all watches and detects whether the variables in the $scope that it listens to change. If the variable changes, the listener function corresponding to the variable is called. The listener function can do a lot of things, such as letting the text inside the HTML display the most recent variable value. Visible, $scope. $digest is a data binding update that can be triggered.

In most cases, Angualrjs automatically calls $scope. $watch () and $scope. $digest () functions, but in some cases we need to call them manually, so it's important to understand how they work.

$scope. $apply () This function executes some code before calling $scope. $digest (). All watches will be detected once and the corresponding listener function will be executed. $scope. $apply () is useful when Angularjs integrates with other JavaScript code.

Next we explain specifically the following $watch (), $digest () and $apply ().

$watch ()

$watch(watchexpression, listener, [objectequality])

Watchexpression: Listener object, either string or function (scope) {}

Listener: Callback function (newval,oldval,scope) {} Executed when listener object is changed

Objectequality: If the depth listener is set to true, it tells angular to check for changes in each property in the monitored object. If you want to monitor an individual element of an array or an object's properties instead of a normal value, you should use it. (default value: false)

$digest ()

Detects all watches in the current scope and sub-scope, because the listener modifies the model (the variable in scope) during execution, $digest () is always called until the model is not changed. When the call exceeds 10 times, the $digest () throws an exception "Maximum iteration limit Exceeded", which prevents the program from entering a dead loop.

$apply ()

$apply([exp])

Exp:string or function (scope) {}

The $apply () life cycle pseudocode is as follows

function $apply (expr) {  try {    return $eval (expr);  } catch (e) {    $exceptionHandler (e);  } finally { c5/> $root. $digest ();  }}
Example

Let's take an example to illustrate $watch, $digest and $apply.

<script>var module = angular.module ("MyApp", []), var myController1 = Module.controller ("Mycontroller", function ( $scope) {$scope. data = {time:new date ()}; $scope. updatetime = function () {$scope. data.time = new Date ();} document.getElementById ("Updatetimebutton"). AddEventListener (' click ', Function () {Console.log ("Update Time clicked "); $scope. Data.time = new Date ();}); </script><body ng-app= "MyApp" ><div ng-controller= "Mycontroller" >    {{data.time}}    <br/ >    <button ng-click= "UpdateTime ()" >update time-ng-click</button>    <button id= " Updatetimebutton "  >update time</button></div></body>

This code will be bound $scope.data.time to the HTML display, and this binding will automatically create a watch to monitor the $scope.date.time changes. In addition, there are 2 buttons, the first button is called through the Ng-click directive to invoke the $scope.updatetime method, then Angularjs automatically executes $scope. $digest () Make the latest time appear in HTML. The second button is to update the time in the HTML by adding a click event through the JavaScript code. But the second button is not working, and its solution is to call $scope at the end of the click event manually. $digest () method, as follows:

document.getElementById ("Updatetimebutton"). AddEventListener (' click ', Function () {Console.log ("Update Time clicked $scope. data.time = new Date (), $scope. $digest ();});

Another workaround is to call $scope. $apply (), as follows:

document.getElementById ("Updatetimebutton"). AddEventListener (' click ', Function () {$scope. $apply (function () { Console.log ("Update Time clicked"); $scope. Data.time = new Date ();});

Reference links

Http://tutorials.jenkov.com/angularjs/watch-digest-apply.html

https://docs.angularjs.org/api/ng/type/$rootScope. Scope

http://yuankeqiang.lofter.com/post/8de51_1454f93

Angularjs in $watch (), $digest () and $apply ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.