two-way binding
Angularjs uses the dirty value detection in the $scope variable to realize the data bidirectional binding. Scope Role:1. Connect the controller and view via data sharing 2. Monitoring and responding to events 3. Dirty value detection and data binding
Because the Ng-model in the input box and the values in the controller implement two-way binding, changing the value of the input box or changing the value in the controller will change the value of both sides accordingly. $ scope object, which we can understand as a scope object in the NG framework, where data and views can be bound to each other, while being able to interact with other $ Scope object is separated from the scope. Of course, $scope can also implement inheritance, in the scope of a controller can inherit its upper level of scope so that it does not exist independently.
Dirty Value Detection
$watch: Angularjs will first compile the expression you declared in {{}} into a function and call the $watch method. $watch method registers a watcher for the current scope, and the Watcher is saved in an array maintained within the scope. $digest: 1. Detection (registered watch function) 2. Notification (the corresponding listener function is triggered)$apply: This method can trigger the $digest method. The execution of the $digest method marks the beginning of a round of digest cycle.
example of a shopping cart
<span style= "Font-family:microsoft yahei;font-size:18px;" ><! DOCTYPE html><span style= "Font-family:microsoft yahei;font-size:18px;" >js in Code:</span>
<span style= "Font-family:microsoft yahei;font-size:18px;" >var mymodule=angular.module (' MyModule ', []) Mymodule.controller (' Cartcontroller ', [' $scope ', function Cartcontroller ($scope) { $scope. Items = [ {name: "angular app", Quantity:1, price:199.00}, {name: " Angular introductory ", Quantity:1, price:139.00}, {name:" Angularjs authoritative tutorial ", Quantity:2, price:84.20} ];//Direct binding event Remove
$scope. remove = function (index) { $scope. Items.splice (index, 1); } ]) </span>
Summary
Imagine the benefits of not having to refresh the page and immediately return the data to the page. have been in the loop to detect whether they are "contaminated", if there is a change, notify the other side followed by change. Is there a problem with this kind of concussion detection, two-way binding used too much on a page, should also be less efficient? Ask the great gods to come and communicate.
AngularJS (2)--ANGULARJS data two-way binding