Angularjs Study Chapter (24)

Source: Internet
Author: User

AngularJSApplication
<HTMLNg-app= "Mynoteapp"><Head><MetaCharSet= "Utf-8"><Scriptsrc= "Http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></Script></Head><Body><DivNg-controller= "Mynotectrl"><H2>My notes</H2><P><textareaNg-model= "message"cols= "Max"rows= "Ten"></textarea></P><P><ButtonNg-click= "Save ()">Save</Button><ButtonNg-click= "Clear ()">Clear</Button></P><P>Number of characters left:<spanNg-bind= "Left ()"></span></P></Div><Scriptsrc= "Mynoteapp.js"></Script><Scriptsrc= "Mynotectrl.js"></Script></Body></HTML>

Application file "Mynoteapp.js":

var app = Angular.module ("Mynoteapp", []);

Controller file "Mynotectrl.js":

function ($scope) {    = "";  $scope.leftfunction() {return - $scope. message.length;};     function () {$scope. Message = "";};    $scope. Save  function() {alert ("Note Saved");};});

The

<html ng-app="Mynoteapp">

<div> is the scope of the controller: ng-controller= "Mynotectrl" in the HTML page:

<div ng-controller="Mynotectrl">

The ng-model directive binds <textarea> to the controller variable message:

<textarea ng-model="message" cols="All" rows="ten"></ TextArea>

Two Ng-click events called the Controller function clear () and Save ():

<button ng-click="Save ()">save</button>
<button ng-click="clear ()">clear</button>

The ng-bind directive binds the controller function left () to <span> to display the remaining characters:

Number of characters left:<span ng-bind="left ()"></span>

The application library file needs to be loaded after AngularJs to execute:

<script src="Mynoteapp.js"></script>
<script src="Mynotectrl.js"></script>
AngularJS Application Architecture

The above example is a complete AngularJS single page Web application (APPLICATION,SPA).

The ng-app=).

The <div> element defines the scope of the AngularJS controller (ng-controller=).

In an application can be by many controllers.

Application files (my ... App.js) defines the application model code.

One or more controller files (my ... Ctrl.js) defines the controller code.

Summary-How does it work?

The NG-APP directive is located under the root element of the app.

For a single page Web app, the root of the app is usually the

One or more Ng-controller directives define the controller for the application. Each controller has his own scope:: A defined HTML element.

AngularJS automatically starts in the HTML domcontentloaded event. If the ng-app instruction is found, AngularJS loads the module in the instruction and compiles the Ng-app as the root of the application.

The root of the application can be an entire page, or a small portion of the page, and if a small part is compiled and executed faster.

Angularjs Study Chapter (24)

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.