Home Web Front-end JS Tutorial How to use Angular data binding mechanism

How to use Angular data binding mechanism

Jun 07, 2018 pm 03:07 PM
angular data binding

This time I will show you how to use the Angular data binding mechanism and what are the precautions for using the Angular data binding mechanism. The following is a practical case, let's take a look.

1.Angular.js extends the browser's event loop

The browser continues to wait for events such as user interaction. After you enter characters in an tag, the callback function of this event performs the DOM operation contained in it in the JS interpreter. After the execution is completed, the browser makes changes to the DOM accordingly. Angular extends this event loop so that it sometimes becomes the execution environment of the angular context.

2.$watch list

$watch can detect changes in the model. Whenever a piece of data is bound to a view, a corresponding $watch will be inserted into the $watch queue. The example is as follows:

controller.js:

app.controller('MainCtrl', function($scope) {
 $scope.people = [...]; // 假设长度为10
});
Copy after login

index.html:

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

where ng-repeat generates 1 $watch and each person generates 2 $watch, the total is (1 2*10), 21 $watches. The generation phase of $watch is when the template loading is completed, which is the linking phase. (Angular is divided into compile and linking phases), Angular will look for each directive (in the above example, ng-repeat and {{}} both belong to directives), and then generate each $watch.

3.$digest loop

When the browser receives angular context-related events, the $digest loop will be triggered. It consists of 2 small loops, one processing the evalAsync queue and the other processing the $watch queue. When $digest cycles, it will traverse the $watch queue to see if any data has been updated. This traversal is called dirty-checkin. If the dirty check finds that $watch has been updated, a new dirty check will be triggered until All $watches are not updated. This ensures that each model will not change.

After more than 10 dirty checks, an exception will be thrown to prevent infinite loops. The DOM will change accordingly after the $digest loop ends. In fact, the literal meaning of $digest is like the process of "digestion", which gradually absorbs all nutrients (changes in $watch).

4. Enter the angular context through $apply

$apply determines whether the event enters the angular context. Use angualr's own directive, such as ng-model, to change the binding. When using data, angular will encapsulate the event into $apply. For example, if you enter the character "w" in the input box of ng-model="name", the event will call $apply("name='w';") to complete the data update in $scope.

Data binding when calling third-party libraries

When calling jquery in angular, the data bound by jquery cannot be updated because jquery does not call $apply and the event does not enter the angular context. As a result, $digest is not executed. An example is as follows:

app.js

 app.directive('clickable', function() {
  return {
   restrict: "E",
   scope: {
    count1: '=',
    count2: '='
   },
   template: '<ul style="background-color: lightblue"><li>{{count1}}</li><li>{{count2}}</li></ul>',
   link: function(scope, element, attrs) {
    element.bind('click', function() {
     scope.count1++;
     scope.count2++;
    });
   }
  }
});
app.controller('MainCtrl', function($scope) {
 $scope.count1= 0;
 $scope.count2= 0;
});
Copy after login

In the example, every time the element is clicked, count1 and count2 are expected to increase by 1, but they actually do not. Actually $scope (ViewModel) has changed, but $digest is not enforced. Modify the click event as follows:

element.bind('click', function() {
 scope.$apply(function() {
   scope.foo++;
   scope.bar++;
 });
})
Copy after login

The expectation is achieved by calling $apply.

5. Summary

angular event binding mechanism is as shown below:

I believe you will read the case in this article You have mastered the method. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

How to use jQuery scroll bar beautification plug-in nicescroll

How to sort json objects and delete the same id data

The above is the detailed content of How to use Angular data binding mechanism. 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)

How to implement data binding function in SwiftUI using MySQL How to implement data binding function in SwiftUI using MySQL Jul 30, 2023 pm 12:13 PM

How to use MySQL to implement data binding function in SwiftUI. In SwiftUI development, data binding can realize automatic updating of interface and data, improving user experience. As a popular relational database management system, MySQL can store and manage large amounts of data. This article will introduce how to use MySQL to implement data binding function in SwiftUI. We will make use of Swift's third-party library MySQLConnector, which provides connections and queries to MySQL data.

How to install Angular on Ubuntu 24.04 How to install Angular on Ubuntu 24.04 Mar 23, 2024 pm 12:20 PM

Angular.js is a freely accessible JavaScript platform for creating dynamic applications. It allows you to express various aspects of your application quickly and clearly by extending the syntax of HTML as a template language. Angular.js provides a range of tools to help you write, update and test your code. Additionally, it provides many features such as routing and form management. This guide will discuss how to install Angular on Ubuntu24. First, you need to install Node.js. Node.js is a JavaScript running environment based on the ChromeV8 engine that allows you to run JavaScript code on the server side. To be in Ub

Detailed explanation of angular learning state manager NgRx Detailed explanation of angular learning state manager NgRx May 25, 2022 am 11:01 AM

This article will give you an in-depth understanding of Angular's state manager NgRx and introduce how to use NgRx. I hope it will be helpful to you!

An article exploring server-side rendering (SSR) in Angular An article exploring server-side rendering (SSR) in Angular Dec 27, 2022 pm 07:24 PM

Do you know Angular Universal? It can help the website provide better SEO support!

How to use PHP and Angular for front-end development How to use PHP and Angular for front-end development May 11, 2023 pm 04:04 PM

With the rapid development of the Internet, front-end development technology is also constantly improving and iterating. PHP and Angular are two technologies widely used in front-end development. PHP is a server-side scripting language that can handle tasks such as processing forms, generating dynamic pages, and managing access permissions. Angular is a JavaScript framework that can be used to develop single-page applications and build componentized web applications. This article will introduce how to use PHP and Angular for front-end development, and how to combine them

A brief analysis of how to use monaco-editor in angular A brief analysis of how to use monaco-editor in angular Oct 17, 2022 pm 08:04 PM

How to use monaco-editor in angular? The following article records the use of monaco-editor in angular that was used in a recent business. I hope it will be helpful to everyone!

A brief analysis of independent components in Angular and see how to use them A brief analysis of independent components in Angular and see how to use them Jun 23, 2022 pm 03:49 PM

This article will take you through the independent components in Angular, how to create an independent component in Angular, and how to import existing modules into the independent component. I hope it will be helpful to you!

Detailed explanation of data binding functions in Vue documentation Detailed explanation of data binding functions in Vue documentation Jun 20, 2023 pm 10:15 PM

Vue is an open source JavaScript framework mainly used for building user interfaces. The core of Vue is data binding, which provides a convenient and efficient way to achieve two-way binding between data and views. Vue's data binding mechanism is handled through some special functions. These functions can help us automatically bind the data in the template to the corresponding properties in the JavaScript object, so that when the properties in the JavaScript object are modified, the data in the template will also automatically

See all articles