Angularjs best Practices two----data binding

Source: Internet
Author: User

In our traditional web framework, such as Rails,controller mixes data from model and blends it into a template to be a view presented to the user. This blending style can lead to Single-way-view. The view can only reflect the model data when the view is rendered. Of course there are also write frameworks that are expected to implement automatic data binding for views and models.

AngularJs uses a different approach, which is not to mix the data with the template and replace the DOM element. ANGULARJS Create a live template as a view. The independent component of the view is replaced by dynamic interpolation, which is probably the most important of angularjs so that we do the Hello World application without writing a single line of JavaScript.

Just include the ANGULARJS source into our HTML file and set a Ng-app feature on the DOM element to complete an application. The Ng-app feature surface contains everything within the feature that belongs to this ANGULARJS application, which is why we can embed a angular application to another web App.

When the view is modified, the variable is replaced by a value, such as the value of a name variable is Soderberg, then "Hello {{name}}" on the View returns "Hello Soderberg".

The automatic binding mechanism of the data gives us the ability to map the view and the state of the model, and any time the model changes will be reflected directly to the view without writing any custom JS code to work. Isn't it amazing?

In the world of MVC, the controller does not have to focus on the mixed rendering of views, which essentially eliminates the concern of separating view and controller logic, making the code testable and enjoyable.

The view does not need to know how to save the object-it only needs to know how to display the object, and the model does not need to interact with the view-it only needs to contain the method and data that handles the view, and the controller's duty is to place the processing logic to bind the view and model. Once the Angularjs value can change, it calls $digest (), and there is a dirty checking process, dirty checking is a valid phone for the model change detection, whenever the value changes, Angularjs will perform a dirty check on the internal loop event to keep all model states consistent.

Simple Data binding

Looking back at the code we just wrote, we bind the name attribute to the input field using the ANGULARJS directive Ng-model, which is contained in the object of the model, which is $scope. This means that any input value can be directly reflected in the model object. $scope object is a JavaScript object, and there are some available properties for the interaction of the view and the controller.

<!DOCTYPE HTML><HTMLNg-app><HeadLang= "en">    <MetaCharSet= "UTF-8">    <title>Simple app</title>    <Scriptsrc= "Https://cdn.bootcss.com/angular.js/1.2.6/angular.js"></Script>   <Scriptsrc= "Helloworld.js"></Script></Head><BodyNg-controller= "Mycontroller"><inputNg-model= "Person.name"type= "text"placeholder= "Your name"><H1>{{Clock}}</H1></Body></HTML>
function Mycontroller ($scope) {    var updateclock=function ()    {        $scope. Clock=new Date ();    }    SetInterval (function () {        $scope. $apply (Updateclock);    },1000);    Updateclock ();};

  

The best data binding is that we are able to use JavaScript references instead of raw objects. Clock changes to Clock.now, the controller code is as follows:

function Mycontroller ($scope) {    $scope. Clock={now:new Date ()};    var updateclock=function ()    {        $scope. Clock.now=new Date ();    }    SetInterval (function () {        $scope. $apply (Updateclock);    },1000);    Updateclock ();};

  

Angularjs best Practices two----data binding

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.