Home Web Front-end JS Tutorial AngularJS two-way data binding principle (detailed tutorial)

AngularJS two-way data binding principle (detailed tutorial)

Jun 08, 2018 pm 05:57 PM
$watch angularjs

This article mainly introduces the application of $watch, $apply and $digest of AngularJS two-way data binding principle. It has certain reference value. Interested friends can refer to it

Introduction

This article is written for AngularJS newbies. If you already have an in-depth understanding of AngularJS’s two-way data binding, just go and read the source code.

Background

AngularJS developers all want to know how two-way data binding is implemented. There are many terms related to data-binding: $watch, $apply, $digest, dirty-checking, etc. How do they work? Let’s start from the beginning

The two-way data binding of AngularJS is forced by the browser

The browser looks beautiful, but in fact, in the aspect of data interaction Son, due to the browser's "inaction", the browser's data refresh has become a problem. Specifically, the browser can easily listen to an event, such as when the user clicks a button or enters something in the input box. It also provides an API for event callback functions. The event callback function will be executed in the javascript interpreter. but the reverse is not so simple. If the data from the background changes, the browser needs to be notified and refreshed. The browser does not provide such a data interaction mechanism. For developers, this It is an insurmountable obstacle, what should I do? AngularJS appears, which implements two-way data binding well through $scope. The principle behind it is $watch, $apply, $digest, dirty-checking

$watch queue ($watch list)

Literally, watch means to observe. Every time you bind something to the browser, a $watch will be inserted into the $watch queue. Imagine that $watch is something that can detect changes in the model it monitors. For example, you have the following code

User: <input type="text" ng-model="user" />
Password: <input type="password" ng-model="pass" />
Copy after login

There is $scope.user, which is bound to the first input box, and there is $scope.pass, which is bound to the second input box. on an input box; then add two $watches to the $watch list:

Create a controllers.js file with the following code:

app.controller(&#39;MainCtrl&#39;, function($scope) {
 $scope.foo = "Foo";
 $scope.world = "World";
});
Copy after login

The corresponding html file, the index.html code is as follows :

Hello, {{ World }}
Copy after login

Here, even if two things are added to $scope, only one is bound to the UI, so only one $watch is generated. Look at the following example:

controllers.js

app.controller(&#39;MainCtrl&#39;, function($scope) {
 $scope.people = [...];
});
Copy after login

The corresponding html file index.html

<ul>
 <li ng-repeat="person in people">
   {{person.name}} - {{person.age}}
 </li>
</ul>
Copy after login

It seems that multiple $watches are generated. Each person has two (one name, one age), and ng-repeat is a loop, so the total of 10 persons is (2 * 10) 1, which means there are 21 $watches. Therefore, every data bound to the browser will generate a $watch. Yes, when was $watch generated? Let’s first review the loading principle of AngularJS

The loading principle of AngularJS:

The template loading of AngularJS is divided into two stages: compilation and linking. During the linking phase, the AngularJS interpreter will look for each directive and generate each required $watch. By the way, $watch is generated at this stage.

Next, let’s start using $digest

$digest loop

Literally, digest means “digestion”. I feel that this name is weird. It is strangely related to dirty-checking, which literally means "dirty checking". It is better not to translate it. The original author's original intention is definitely not this, it can only be understood but not expressed in words!

$digest is a loop, what is it doing in the loop? $digest is iterating over our $watch. $digest asks $watch one by one - "Hey, has the data you observed changed?"

This traversal is the so-called dirty-checking. Now that all $watches have been checked, we have to ask: Has $watch been updated? If at least one has been updated, the loop will be triggered again until all $watches are unchanged. This ensures that each model will not change again. Remember that if the loop exceeds 10 times, it will throw an exception to avoid an infinite loop. When the $digest loop ends, the DOM changes accordingly.

Look at the code, for example: controllers.js

app.controller(&#39;MainCtrl&#39;, function() {
 $scope.name = "Foo";
 $scope.changeFoo = function() {
   $scope.name = "Bar";
 }
});
Copy after login

The corresponding html file, index.html

{{ name }}
<button ng-click="changeFoo()">Change the name</button>
Copy after login

There is only one $watch here, because ng-click does not generate $ watch (the function will not change).

$digest execution process is:

  1. Press the button in the browser;

  2. The browser receives an event , enter the angular context.

  3. The $digest loop begins to execute, querying whether each $watch changes.

  4. Because the $watch monitoring $scope.name reports a change, it will force another $digest cycle.

  5. The new $digest loop does not detect changes. At this time, the browser takes back control and updates the DOM corresponding to the new value of $scope.name.

We can see an obvious shortcoming of AngularJS: every event entering the angular context will execute a $digest loop. Even if you only enter a letter, $digest will traverse the entire page. of all $watch.

$apply’s application

Angular context 是整个Angular的上下文,也可以把它理解为Angular容器,那么,是谁来决定哪些事件可以进入 Angular Context,哪些事件又不能进入呢? 其控制器在 $apply手上。

如果当事件触发时,调用$apply,它会进入angular context,如果没有调用就不会进入。你可能会问:刚才的例子并没有调用$apply,这是怎么回事呢?原来,是Angular背后替你做了。当点击带有ng-click的元素时,事件就会被封装到一个$apply调用中。如果有一个ng-model="foo"的输入框,当输入一个字母 f 时,事件就会这样调用,$apply("foo = 'f';")。

$apply的应用场景

$apply是$scope的一个函数,调用它会强制一次$digest循环。如果当前正在执行$apply循环,则会抛出一个异常。

如果浏览器上数据没有及时刷新,可以通过调用$scope.$apply() 方法,强行刷新一遍。

通过 $watch 监控自己的$scope

<!DOCTYPE html>
<html ng-app="demoApp">
<head>
 <title>test</title>
 <!-- Vendor libraries -->
  <script src="lib/jquery-v1.11.1.js"></script>
  <script src="lib/angular-v1.2.22.js"></script>
  <script src="lib/angular-route-v1.2.22.js"></script>
</head>
<body> 
 <p ng-controller="MainCtrl" >
  <input ng-model="name" />
  Name updated: {{updated}} times.
 </p> 
 <script >
  var demoApp = angular.module(&#39;demoApp&#39;,[]); 
  demoApp.controller(&#39;MainCtrl&#39;, function($scope) {
  $scope.name = "Angular";
  $scope.updated = -1;
  $scope.$watch(&#39;name&#39;, function() {
  $scope.updated++;
 });
});
 </script>
 </body>
</html>
Copy after login

代码说明:

当controller 执行到 $watch时,它会立即调用一次,所以把updated的值设为 -1 。 上输入框中输入字符发生变化时,你会看到 updated 的值随之变化,而且能显示变化的次数。

$watch 检测到的数据变化

小结

我们对 AngularJS的双向数据绑定有了一个初步的认识,对于AngularJS来说,表面上看操作DOM很简单,其实背后有 $watch、$digest 、 $apply 三者在默默地起着作用。这个遍历检查数据是否发生变化的过程,称之为:dirty-checking。 当你了解了这个过程后,你会对它嗤之以鼻,感觉这种方法好low 哦。 确实,如果一个DOM中有 2000- 3000个 watch,页面的渲染速度将会大打折扣。

这个渲染的性能问题怎么解决呢?随着ECMAScript6的到来,Angular 2 通过Object.observe 极大地改善$digest循环的速度。或许,这就是为什么 Angular 团队迫不及待地推出 Angular 2 的原因吧。

上面是我整理给大家的,希望今后会对大家有帮助。

相关文章:

在vue-cli中如何配置babel配置文件

使用node.js实现抖音自动抢红包功能

使用webpack打包处理bundle.js文件过大的问题

The above is the detailed content of AngularJS two-way data binding principle (detailed tutorial). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

The latest 5 angularjs tutorials in 2022, from entry to mastery The latest 5 angularjs tutorials in 2022, from entry to mastery Jun 15, 2017 pm 05:50 PM

Javascript is a very unique language. It is unique in terms of the organization of the code, the programming paradigm of the code, and the object-oriented theory. The issue of whether Javascript is an object-oriented language that has been debated for a long time has obviously been There is an answer. However, even though Javascript has been dominant for twenty years, if you want to understand popular frameworks such as jQuery, Angularjs, and even React, just watch the "Black Horse Cloud Classroom JavaScript Advanced Framework Design Video Tutorial".

Use PHP and AngularJS to build a responsive website to provide a high-quality user experience Use PHP and AngularJS to build a responsive website to provide a high-quality user experience Jun 27, 2023 pm 07:37 PM

In today's information age, websites have become an important tool for people to obtain information and communicate. A responsive website can adapt to various devices and provide users with a high-quality experience, which has become a hot spot in modern website development. This article will introduce how to use PHP and AngularJS to build a responsive website to provide a high-quality user experience. Introduction to PHP PHP is an open source server-side programming language ideal for web development. PHP has many advantages, such as easy to learn, cross-platform, rich tool library, development efficiency

Build web applications using PHP and AngularJS Build web applications using PHP and AngularJS May 27, 2023 pm 08:10 PM

With the continuous development of the Internet, Web applications have become an important part of enterprise information construction and a necessary means of modernization work. In order to make web applications easy to develop, maintain and expand, developers need to choose a technical framework and programming language that suits their development needs. PHP and AngularJS are two very popular web development technologies. They are server-side and client-side solutions respectively. Their combined use can greatly improve the development efficiency and user experience of web applications. Advantages of PHPPHP

Vue component communication: using $watch for data monitoring Vue component communication: using $watch for data monitoring Jul 07, 2023 am 11:09 AM

Vue component communication: using $watch for data monitoring In Vue development, component communication is a common requirement. Vue provides a variety of ways to implement communication between components. One of the common ways is to use $watch for data monitoring. This article will introduce the usage of $watch and give corresponding code examples. Vue's instance object provides the $watch method for monitoring data changes. $watch accepts two parameters: the property name of the data to be monitored, and the callback function. When listening to data

Build a single-page web application using Flask and AngularJS Build a single-page web application using Flask and AngularJS Jun 17, 2023 am 08:49 AM

With the rapid development of Web technology, Single Page Web Application (SinglePage Application, SPA) has become an increasingly popular Web application model. Compared with traditional multi-page web applications, the biggest advantage of SPA is that the user experience is smoother, and the computing pressure on the server is also greatly reduced. In this article, we will introduce how to build a simple SPA using Flask and AngularJS. Flask is a lightweight Py

How to use AngularJS in PHP programming? How to use AngularJS in PHP programming? Jun 12, 2023 am 09:40 AM

With the popularity of web applications, the front-end framework AngularJS has become increasingly popular. AngularJS is a JavaScript framework developed by Google that helps you build web applications with dynamic web application capabilities. On the other hand, for backend programming, PHP is a very popular programming language. If you are using PHP for server-side programming, then using PHP with AngularJS will bring more dynamic effects to your website.

Use PHP and AngularJS to develop an online file management platform to facilitate file management Use PHP and AngularJS to develop an online file management platform to facilitate file management Jun 27, 2023 pm 01:34 PM

With the popularity of the Internet, more and more people are using the network to transfer and share files. However, due to various reasons, using traditional methods such as FTP for file management cannot meet the needs of modern users. Therefore, establishing an easy-to-use, efficient, and secure online file management platform has become a trend. The online file management platform introduced in this article is based on PHP and AngularJS. It can easily perform file upload, download, edit, delete and other operations, and provides a series of powerful functions, such as file sharing, search,

Introduction to the basics of AngularJS Introduction to the basics of AngularJS Apr 21, 2018 am 10:37 AM

The content of this article is about the basic introduction to AngularJS. It has certain reference value. Now I share it with you. Friends in need can refer to it.

See all articles