Home > Web Front-end > JS Tutorial > Understanding Angular data two-way binding_AngularJS

Understanding Angular data two-way binding_AngularJS

WBOY
Release: 2016-05-16 15:20:35
Original
1077 people have browsed it

AngularJS is an excellent front-end JS framework that has been used in many Google products. AngularJS has many features, the most core of which are: MVVM, modularization, automated two-way data binding, semantic tags, dependency injection, etc.

1. What is two-way data binding
Angular implements a two-way binding mechanism. The so-called two-way binding is nothing more than that the operations from the interface can be reflected in the data in real time, and the changes in the data can be displayed in the interface in real time.

The simplest example is this:

<div ng-controller="CounterCtrl">
  <span ng-bind="counter"></span>
  <button ng-click="counter++">increase</button>
</div>function CounterCtrl($scope) {
  $scope.counter = 1;
}
Copy after login

This example is very simple. Every time the button is clicked, the number on the interface increases by one.

2. Principle of two-way data binding
1. Deep understanding
Enables users to control the display order of mobile phone lists. Dynamic sorting can be implemented by adding a new model property, integrating it with an iterator, and letting data binding do the rest.

Template (app/index.html)

Search: <input ng-model="query">
Sort by:
<select ng-model="orderProp">
 <option value="name">Alphabetical</option>
 <option value="age">Newest</option>
</select>


<ul class="phones">
 <li ng-repeat="phone in phones | filter:query | orderBy:orderProp">
  {{phone.name}}
  <p>{{phone.snippet}}</p>
 </li>
</ul>

Copy after login

The following changes were made in index.html:

First, a