AngularJS Quick Start Guide 18: Application

11170 단어
Time to create a real AngularJS Single Page Application (SPA).

An example AngularJS application


You've learned enough to create your first AngularJS application:


Program code is explained



<html ng-app="myNoteApp">
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js">script>
<body>

<div ng-controller="myNoteCtrl">

<h2>My Noteh2>

<p><textarea ng-model="message" cols="40" rows="10">textarea>p>

<p>
<button ng-click="save()">Savebutton>
<button ng-click="clear()">Clearbutton>
p>

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

div>

<script src="myNoteApp.js">script>
<script src="myNoteCtrl.js">script>

body>
html>

run
Application file "myNoteApp.js"code:

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

Controller file "myNoteCtrl.js"code:

app.controller("myNoteCtrl", function($scope) {
    $scope.message = "";
    $scope.left  = function() {return 100 - $scope.message.length;};
    $scope.clear = function() {$scope.message = "";};
    $scope.save  = function() {alert("Note Saved");};
});

The element is specified as the container of the AngularJS application: ng-app="myNoteApp":

<html ng-app="myNoteApp">

The element in the page specifies the scope of the controller: ng-controller="myNoteCtrl":

<div ng-controller="myNoteCtrl">

The ng-model directive binds the element to the controller's message variable:

<textarea ng-model="message" cols="40" rows="10">textarea>

The two ng-click events call the clear() and save() functions in the controller respectively:

<button ng-click="save()">Savebutton>
<button ng-click="clear()">Clearbutton>

The ng-bind directive binds the controller's left() function to the element to display the remaining number of characters that can be entered:

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

The application libraries are added to the page (after the AngularJS library reference):

<script src="myNoteApp.js">script>
<script src="myNoteCtrl.js">script>


AngularJS application framework


With the above example, you already have a real AngularJS application framework (or scaffolding as it is called), which is a Single Page Application (SPA).
The HTML element where the ng-app directive resides is defined as the container for the AngularJS application.
The element on which the directive ng-controller resides defines the scope of the AngularJS application controller.
You can have multiple controllers in an application.
An application file (eg my...App.js) defines the code for an application module.
One or more controller files (eg my...Ctrl.js) define the code for the controller.

How does it work?


The ng-app directive is placed on the root element of the application.
For a single page application (SPA), the root element of the application is the element.
One or more ng-controller directives define different controllers in the application. Each controller has its own scope: the HTML element that contains the directive definition.
AngularJS is automatically called when the HTML's DOMContentLoaded event is executed. If the ng-app directive is found, AngularJS will load all modules named after the directive and compile the DOM inside the root element of the application defined by ng-app.
The root of the application can be the entire page or a small part of the page. The smaller the part of an AngularJS application definition, the faster the application compiles and executes.


Previous Chapter - AngularJS Quick Start Guide 17: Includes


Next Chapter - AngularJS Quick Start Guide 19: Sample Code









좋은 웹페이지 즐겨찾기