AngularJS-quick start

Source: Internet
Author: User

Always go to a wiki or encyclopedia to get a glimpse of it when you first Touch.
They'll tell me about mvvm, two-way data binding, dependency injection, and so on, and I think these nouns are good grades, and then I'm probably not going to use this stuff.

What is angularjs?

A client-side technology written entirely using Javascript. Working with other historic web technologies (HTML, CSS, and Javascript) makes Web application development easier and faster than ever.


Without considering those nouns, this article helps you quickly understand the basic usage of angularjs.

Thanks to @myqianlan Chicory reminder, This article uses the ANGULARJS version for 1.2.x,1.3.x no longer declares the controller in the manner mentioned in this Article.


Let's start with this paragraph:

 <div ng-app= "data-ng-init= "name= ' kavlez" "> <p>name: <input type= "text "ng-model=" name "></p> <p  Ng-bind= "name" ></p> {{name}} { {6+8}} </div>          

    • Ng-app: defines a ANGULARJS application that is included in the app.
    • Ng-init: Initializes application Data.
    • Ng-model: binds data to the current app.
    • Ng-bind: Displays the variables defined in the app in the label


Angularjs directives are either ng prefaced or can be used at the data-ng beginning. The expression for Angularjs is expressed in two curly braces:{{expression}}
This is the most basic function of angularjs: data binding .


We can bind and display the various types:

  • Number

    <div ng-app="" ng-init="quantity=1;cost=5"> <p>Total : ¥{{ quantity * cost }}</p></div>
  • String

     <div ng-app=ng-init=  "firstname= ' Kavlez '; lastname= ' Jin '" > < p>the name is {{firstName + "" + lastName}}</p></div>      
  • Object

     <div ng-app=ng-init= <p>the name is {{person.lastname}}</p></div>         
  • Array

    PS: can be used ng-repeat to iterate

    <div ng-app="" ng-init="myArr=[0,2,4,6,8]"> <p ng-repeat="element in myArr"> {{ element }} </p></div>

Controllers

We use a controller to control a angularjs Application.
By ng-controller defining a controller, declare that all the elements that it contains belong to it.

For example:

<Divng-app=""Ng-controller="personcontroller" ><H1>{{person.name + ' is a ' +person.job}}</H1><H1>{{person.sayhi ()}}</h1></ div><script> function  Personcontroller ' Kavlez ' $scope. person.job= ' brogrammer ' $scope. person.sayhi = function () { return  "hi! I ' m "+ $scope. person.name + ", i ' m a (an) "+ $scope. person.job;}} </script>         


of course, We should use a more efficient organizational Approach. The
Angularjs supports the definition of controller in other JS files, such as the example above can be changed to:

<div ng-app="" ng-controller="personController"> <h1>{{ person.name +‘ is a(an) ‘+person.job }}</h1></div><script type="text/javascript" src="js/app.js"></script>

Module

So far, define a way to use the angular app ng-app="" .
yes, but the variables and functions under this app are global and can cause controller pollution (pollute).
In general, we use the module to organize the app. We can ng-app="名称" angular.modele() declare a module by Or.
The method has two parameters:

    1. The name of the module
    2. Dependency List

For example, The previous example could be changed to:

<div ng-app="myApp" ng-controller="fighterController"> <script> var myApp = angular.module("myApp",[]); myApp.controller("fighterController", function($scope) { //.. } </script></div>


If you put the app and controller into two files defined separately, then:

<div ng-app="myApp" ng-controller="fighterController"> <script src="myApp.js"></script> <script src="myAppControllers.js"></script></div>


Myapp.js:

var myApp = angular.module("myApp",[]);

Myappcontrollers.js:

myApp.controller("fighterController", function($scope) { //..}

Filter

This can be added to the expression, changing the output format of the Expression.
We can use the following filters:

Filter Description
Uppercase Turn capital
lowercase Turn lowercase
Currency Turn currency format
By Sort by specified attribute
Filter Filter by the specified keyword

The first 3 can be used directly in an expression, such as:

{{name | uppercase}}{{name | lowercase}}{{total | currency}}

Filter and by-do are typically used for arrays:

<Divng-app=""Ng-controller="fightercontroller" ><InputNg-model="search"/><Ul><Ling-repeat="f in fighters|filter:search" > {{f}}</Li></ul> <script>  function  Fightercontroller ' Ryu ', Country: ' Ken ', country:  ' USA '}, {name: ' Chun Li ', country: ' GuiLe ', country: ' USA '}, {name: ' Zangief ', country: ' Russia '}]; } </script></ div>             


of course, the data will not be placed in a controller.
We can $http request the data and bind it.
The above example can be changed to:

function fighterController($scope,$http) { var url = ‘请求地址‘; $http.get(url).success(function(response) { $scope.fighters = response; })}

AngularJS-quick start

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.