Data binding for AngularJS

Source: Internet
Author: User

Differences between one-way binding (Ng-bind) and two-way binding (Ng-model)

ng-bindOne-way data binding ( $scope -> view ), used for data display, abbreviated form is {{}} .

1
<ng-bind="val" ></span>

The difference between the two is that the page is not loaded {{val}} and will be displayed directly to the page until Angular renders the bound data (this behavior is likely to be visible to the {{val}} user), while the ng-bind data is displayed after the Angular rendering is complete.

ng-modelis two-way data binding ( $scope -> view view -> $scope and), which is used to bind table elements such as values that change.

1
<type=ng-model="Val"/>
The principle of bidirectional data binding

Two-way data binding means that when view any data changes are automatically fed back to scope the data, the scope view data in it is updated to the most recent value when the model changes. Obviously, this requires a monitoring.

In fact, AngularJS did set up scope a listening queue on the model behind the scenes to listen for data changes and update view .

Each time you bind a thing to the view top, AngularJS will $watch insert a joins into the team to $watch detect model if there are any changes in its monitoring.

The loop is triggered when the browser receives angular context an event that can be processed $digest . $digestwill traverse all of the $watch .

An updated operation (at least two times $digest () loop is triggered)

For example click , one operation:

1
<button ng-click=1</button>
    • Press the button
    • The browser receives an event and entersangular context
    • $digestLoop begins execution, querying for each $watch change
    • Because the $scope.val monitoring $watch reports a change, it forces a loop to be executed again $digest .
    • The new $digest loop does not detect a change.
    • The browser takes back control and updates the $scope.val corresponding section with the new value DOM .
How many times will the $digest loop run?

$digestLoops do not run only once. At the end of the current cycle, it executes a loop again to check for models changes.

This is the Dirty Check ( Dirty Checking ), which is used to handle listener changes that can occur when a function is executed model . Therefore $digest , the loop continues to run until it model no longer changes, or $digest the number of cycles is reached 10 ( 10 an exception is thrown after more than one time, preventing an infinite loop).

When $digest the loop is over, it DOM changes accordingly.

Differences between $apply () and $digest ()

$applyis $scope (or is direcvie link ) a function of the function in which scope it is called to force a $digest loop (unless the loop is currently being executed, in which case an exception is thrown, which is the flag we do not need to execute there $apply ).

$apply()And $digest() there are two differences.

1) The most straightforward difference is that $apply it can take a parameter, it can accept a function, and then after applying the data, call this function. Therefore, it is generally possible to write code in this code when integrating non-Angular frameworks (such as jquery).

2) when invoked, $digest only the monitoring on the current scope and its child scopes is triggered, but when invoked $apply , all monitoring on the scope tree is triggered.

When do I manually invoke the $apply () method?

Depends on whether the Angular context () is in the environment angular context .

Typical scenarios where calling $apply() methods are required are:

1) Use JavaScript setTimeout() to update ascope model

2) Set an event with DOM listener the directive and listener modify some of themodels

Scene One
 1 
2
3
Span class= ' line ' >4
5
6
7
  $scope. setmsg = function () {
SetTimeout (function () {
$scope. Message = Span class= "string" > ' Hello World ';
console.log ( 2000);
$scope. Setmsg ();

Running this example, you will see that after two seconds, the console does show that it has been updated model , however, it view is not updated.

In the $scope.getMessage join $apply() method.

1
2
3
4
5
6
7
8
function() {  
SetTimeout (function
$scope. $apply (function

Console.log (


}

It's OK to run again.

However, in AngularJS you should try to use $timeout service instead setTimeout() , as the former will help you invoke $apply() it, so you don't need to call it manually.

1
2
3
4
$timeout (function() {

Console.log (
2000)
Scene Two

Implement a click directive that resembles the following features:

1
<button ng-click=1</button>

directiveis prepared as follows:

 1 
2
3
4
5
6
7
 app.directive (function () {
return function Element.on (function () {
scope.val++;
};
});
/span>

As with the result of the scene one, this time, click on the button, the number on the interface will not increase. But look at the debugger and find that the data has actually increased.

scope.val++;Add or OK after a line scope.$apply(); scope.$digest(); .

Two forms of the $apply () method

1) No reference

1
$scope. $apply ()

2) With reference

1
2
3
$scope. $apply (function() {
...
})

You should always use a function method that takes one as a parameter $apply() . This is because this is wrapped into a block when it is passed in, so that if an function $apply() function try…catch exception occurs, the exception is $exceptionHandler service processed.

Imagine if a alert box shows an error to the user, then a third-party library makes a network call and then fails, and if it is not encapsulated $apply inside, Angular will never know the failure, and the alert box will never bounce out.

Use $watch in AngularJS

Commonly used methods of use:

1
2
3
4
5
' HTF ';
$scope. $watch (function(newvalue, OldValue) {

$scope. updated++;
});

The $watch() second parameter passed in is a callback function that is called when the value of the function name changes.

If you are listening to an object, you need a third parameter:

1
2
3
4
5
' HTF ';
$scope. $watch (function(newvalue, OldValue) {

$scope. updated++;
true);

Indicates that the value of the object is compared instead of the reference, and if the third argument is not added, true the data.name action is not triggered when it changes, because the same reference is referenced.

Reference
    1. Understanding $watch, $apply and $digest--Understanding the data binding process
    2. Understanding $apply () and $digest () in angular
    3. Angular meditation (i) Data binding
    4. Build your own Angularjs, part one: Scope and digest

Ext.: http://huangtengfei.com/2015/09/data-bind-of-angularjs/

Data binding for AngularJS

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.