Angularjs, angularjs Chinese community

Source: Internet
Author: User

Angularjs, angularjs Chinese community
Angularjs aims to reduce the complexity of developing applications using AJAX, making it easy to create, test, expand, and maintain programs. The following are some core concepts in angularjs.
1. Client TemplateMulti-page applications generate HTML by assembling and splicing data on the server, and then output it to the browser. Angularjs is different in that it transmits templates and data to the browser and then assembles them on the browser side. The browser's role programming only provides the static resources of the template and the data required by the template.

Hello.html
Controllers. js
function HelloController($scope) {    $scope.greeting = { text: 'Hello' };}


2. Model View Controller (MVC)The core concept of MVC is to clearly separate and manage data (models) between codes, apply Program Logic (controllers), and present data to users (Views ). In Angular applications, a view is DOM, a controller is a Javascript class, and model data is stored in object attributes.
3. Typical DOM operations for Data Binding: After data processing is completed, you can set innerHTML on the element to insert the result to the expected DOM. This is highly repetitive and ensures that the data is in the correct state on the interface and javascript attributes. Angular only declares some of the attributes of the silver snake in the interface to Javascript, so that they can be automatically synchronized.
Controllers. js
function HelloController($scope) {    $scope.greeting = { text: 'Hello' };}
The input value (user input) is bound to greeting. text and displayed in <p>. Binding is bidirectional. You can also set the value of $ scope. greeting. text and automatically synchronize it to the input box and double braces ({{}}).
4. Dependency Injection$ Scope object data binding is automatically passed to developers through the HelloController constructor. $ scope is not the only one we need. You can also add a $ location object, for example:
function HelloController($scope, $location) {$scope.greeting = { text: 'Hello' };// use $location for something good here...}
Through Angular's dependency injection system, we can follow the dimit rule (minimum knowledge principle) and focus only on what we need most.
5. CommandsAngular includes a powerful DOM conversion engine that enables developers to expand HTML syntax. In the previous instance, we saw that {} was bound with data, and ng-controller was used to specify which controller to serve which view, ng-model binds an input box to the model section. We call it an HTML extension command. Angular contains many identifiers to define views. These identifiers can define common views as templates. In addition, developers can write their own identifiers to expand the HTML template.
Shopping cart example:
<!DOCTYPE html>
<Html ng-app>
Ng-app tells Angular to manage the part of the page. Ng-app can also be placed on <div> as needed
<Body ng-controller = "CartController">
The Javascript class is called a controller, which can manage everything in the corresponding page area.
<Div ng-repeat = "item in items">
Ng-repeat indicates that the DOM in the DIV is copied once for each element in the items array, and the item is set as the current element and can be used in the template.
<Span >{{ item. title }}</span>
Expression {item. title} retrieves the current item in the iteration and inserts the title attribute value of the current item into the DOM.
<Input ng-model = "item. quantity">
Ng-model defines the data binding between the input field and item. quantity
<Span >{{ item. price | currency }}</span>
<Span >{{ item. price * item. quantity | currency }}</span>
The unit price and total price are formatted as USD. Format the dollar format using Angular's currency filter.
<Button ng-click = "remove ($ index)"> Remove </button>
Remove Data and the corresponding DOM through the iteration sequence $ index of ng-repeat (bidirectional binding feature)
Function CartController ($ scope ){
CartController manages the logic of this shopping cart. $ scope is used to bind data to elements on the interface.
$ Scope. items = [{title: 'paint pots', quantity: 8, price: 3.95}, {title: 'polka dots', quantity: 17, price: 12.95}, {title: 'pebbles ', quantity: 5, price: 6.95}];
By defining $ scope. items, we have created a virtual data that represents a collection of items in the user's shopping cart. The shopping cart cannot only work in the memory, but also needs to notify the server of persistent data.
$ Scope. remove = function (index) {$ scope. items. splice (index, 1 );};
The remove () function can be bound to the interface, so we also add it to $ scope.







Establish an AngularJS Environment

Pay attention to the following key areas,
1. Add ng-app
2. Introduce angularjs.
See angularjs.org/here for details.
<! Doctype html>
Establish an AngularJS Environment

Pay attention to the following key areas,
1. Add ng-app
2. Introduce angularjs.
See angularjs.org/here for details.
<! Doctype html>

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.