AngularJS Quick Start Guide 18: Application
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
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.