Angular JS Notes

Source: Internet
Author: User

?

1. Boot program

?

Start booting a program with Ng-app: tags the scope of the Angularjs app

    1. <!doctype html>
    2. <htmllang= "en"ng-app>

Double-brace Binding expression:

    1. <p>nothing here {{' yet ' + '! '}} </P>

?

2. Views and templates

?

The core is the MVC pattern

Principle: In Angularjs, a view is a map after the model is rendered through an HTML template

?

HTML Templates :

Contains Ng-controller directives, and ng-repeat directives

  1. <htmlng-app>
  2. <head>
  3. ?? ...
  4. ?? <scriptsrc= "lib/angular/angular.js" ></script>
  5. ? ? <scriptsrc= "js/controllers.js" ></script>
  6. </head>
  7. <bodyng-Controller= "Phonelistctrl">
  8. ?
  9. ?? <ul>
  10. ???? <ling-repeat= "phone in phones">
  11. ?????? {{Phone.name}}
  12. ???? <p>{{phone.snippet}}</p>
  13. ???? </li>
  14. ?? </ul>
  15. </body>
  16. </html>

Ng-repeat Syntax: object variable in object array

?

Models and controllers :

$scope is a scope, a successor to the root scope, which is used by the name of Di.

Phones is the model.

    1. function Phonelistctrl ($scope) {
    2. ?? $scope. Phones = [
    3. ???? {"Name": "Nexus S",
    4. ?????" Snippet ":" Fast just got faster with Nexus S. "},
    5. ???? {"Name": "Motorola XOOM?") With Wi-Fi ",
    6. ?????" Snippet ":" The next, next Generation tablet. "},
    7. ???? {"Name": "MOTOROLA XOOM?",
    8. ?????" Snippet ":" The next, next Generation tablet. "}
    9. ??];
    10. }

?

The scope theory of Angularjs is very important:

    1. A scope can be seen as a glue that works together as a template, model, and controller.
    2. Angularjs uses scopes, as well as information in templates, data models, and controllers.
    3. These can help separate the model from the view, but they are really synchronous! Any changes to the model are immediately reflected in the view, and any changes to the view are immediately reflected in the model.

?

3. Filter

?

Conditional filtering on the page:

Search: <input ng-model= "Query" >

?

  1. <ulclass= "Phones" >
  2. ???????? <ling-repeat= "Phone in phones | filter:query" >
  3. ?????????? {{Phone.name}}
  4. ???????? <p>{{phone.snippet}}</p>
  5. ???????? </li>
  6. </ul>

?

Explain:

    • In this code, the data name entered by the user in the input box, called query, is immediately used as the list iterator (phone in phones | Filter:query ') The input of its filter. When the data model causes iterator input to change, iterators can efficiently update the DOM to reflect the latest state of the data model.
    • Use the filter filter: The filter function uses the value of query to create a new array that contains only matching query records.

      Ngrepeat automatically updates the view based on the cell phone record data array generated by the filter filter.

      The whole process is transparent to the developer.

?

4. Bidirectional binding

?

Use Orderprop as a model binding whose value is applied to the filter and used as a sort

Orderby:orderprop: Sort by the attributes selected by Orderprop.

  1. <selectng-model= "orderprop" >
  2. ?? <optionvalue= "Name" > Alphabetical</option>
  3. ?? <optionvalue= "Age" > Newest</option>
  4. </Select>
  5. ?
  6. ?
  7. <ulclass= "Phones" >
  8. ?? <ling-repeat= "Phone in phones | filter:query | orderby:orderprop">
  9. ???? {{Phone.name}}
  10. ???? <p>{{phone.snippet}}</p>
  11. ?? </li>
  12. </ul>

?

Initialization

$scope. Orderprop = ' age ';

?

5. XHR and Dependency Injection

?

A series of objects that begin with di:$. such as: $scope, $http.

    1. function Phonelistctrl ($scope, $http) {
    2. ?? $http. Get (' Phones/phones.json '). Success (function (data) {
    3. ???? $scope. phones = data;
    4. ??});
    5. ?
    6. ?? $scope. Orderprop = ' age ';
    7. }

?

To avoid compressing the di:

    1. Phonelistctrl. $inject = [' $scope ', ' $http '];

?

Another method:

    1. var Phonelistctrl = [' $scope ', ' $http ', function ($scope, $http) {/* Constructor body */}];

?

6. Links

?

ng-src= "{{Phone.imageurl}}" >

?

7. Routing and Multi-view

?

Angular JS Notes

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.