ANGULARJS Basic Magic Data binding (binding)

Source: Internet
Author: User

ARCCOSXY Reprint Please specify the Source: http://www.cnblogs.com/arccosxy/

In the previous section, we declare a scope variable in JS and then access it directly in HTML, which is very cool. But Angularjs is able to provide more magical and fun things. Data binding is one of the most important topics, and then I'll cover the most important parts of data binding, and you'll understand what it's used for after learning.

To understand its concept, declare a scope variable as model (not tube model, later). Modify the Maincontroller.js as follows:

function ($scope) {    = "";});

Add some HTML code to our page:

<!DOCTYPE HTML><Head>    <title>Learning AngularJS</title>    <Scriptsrc= "Https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"type= "Text/javascript"></Script>    <Scriptsrc= "App.js"type= "Text/javascript"></Script>    <Scriptsrc= "Maincontroller.js"type= "Text/javascript"></Script></Head><Body>    <DivID= ' content 'Ng-app= ' Mytutorialapp 'Ng-controller= ' Maincontroller '>            <inputtype= ' text 'Ng-model= "Inputvalue">{{Inputvalue}}</Div></Body></HTML>

In the above code we add a simple text input box and use Ng-model to bind it to the scope variable Inputvalue. Note that we are not wrapping Inputvalue with {{}} in the ng-* tag. In addition, we output the scope variable inputvalue to the page.

The above does this so that we bind the input value to the scope variable. This means that whenever the input value has changed, the bound scope variable is updated at the same time. Open our page in a browser, enter something in the input box, and you'll see something like this:

When you enter a worthwhile time, the scope variable is updated in real time, which can be intuitively seen on the screen, quite magical! Now you've learned how to synchronize changing elements and values on the same page. Let's move on to a more complex example so you can better understand what data binding does. Update Maincontroller.js as follows:

App.controller ("Maincontroller",function($scope) {$scope. Selectedperson= 0; $scope. Selectedgenre=NULL; $scope. People=[{ID:0, Name:' Leon ', music: [' Rock ',                ' Metal ',                ' Dubstep ',                ' Electro ']}, {ID:1, Name:' Chris ', music: [' Indie ',                ' Drumstep ',                ' Dubstep ',                ' Electro ']}, {ID:2, Name:' Harry ', music: [' Rock ',                ' Metal ',                ' Thrash Metal ',                ' Heavy Metal ']}, {ID:3, Name:' Allyce ', music: [' Pop ',                ' RnB ',                ' Hip Hop '            ]        }    ];});

We have declared three scope variables, and Selectedperson is used to record the selected person object. Selectedgenre the genre used to record music. Finally, we used an array of objects to record people's musical tastes. We will implement two drop-down selection boxes. A selection of people, and another because the bindings will be automatically updated to let you choose this person's music genre. Add HTML elements to the page as follows:

<!DOCTYPE HTML><Head>    <title>Learning AngularJS</title>    <Scriptsrc= "Https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"type= "Text/javascript"></Script>    <Scriptsrc= "App.js"type= "Text/javascript"></Script>    <Scriptsrc= "Maincontroller.js"type= "Text/javascript"></Script></Head><Body>    <DivID= ' content 'Ng-app= ' Mytutorialapp 'Ng-controller= ' Maincontroller '>        <SelectNg-model= ' Selectedperson 'ng-options= ' Obj.nameFor obj in people '></Select>        <SelectNg-model= ' selectedgenre '>            <optionng-repeat= ' labelIn People[selectedperson.id].music '>{{label}}</option>        </Select>    </Div></Body></HTML>

Here are some new things. First, we added a select input box with a ng-options tag, which allows us to populate the selection input box with the value of the People scope variable, which uses the Name property of the person object as the label for the selection input box.

The second selection input box is populated in a different way. "Ng-repeat" seems to me to be a powerful feature in Angularjs. Ng-repeat allows you to repeat HTML elements based on arrays or collections of objects. In the above HTML, Ng-repeat will create an option based on the Selectedpersons ID value for each genre of music in people, and the Selectedpersons ID value is determined by the selection of the first selection input box.

Note: Ng-repeat can be used with any HTML element, and you can try it yourself.

The {{label}} in the option tag is used to output the label declared in the Ng-repeat option. Open your browser and give it a try. The effect is as follows:

ANGULARJS Basic Magic Data binding (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.