AngularJS Tutorial(12)from w3school



An AngularJS module defines an application.

A module is a container for the different parts of an application.

All application controllers should belong to a module.


A Module With One Controller

This application ("myApp"), has one controller ("myCtrl"):

AngularJS Example

< !DOCTYPE html >
< html >
< script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js" > < /script >
< body >

< div ng-app= "myApp" ng-controller= "myCtrl" >
{{ firstName + " " + lastName }}
< /div >

< script >
var app = angular.module( "myApp", []);
app.controller( "myCtrl", function($scope) {
    $scope.firstName = "John";
    $scope.lastName = "Doe";
});
< /script >

< /body >
< /html >

Try it Yourself »

Modules and Controllers in Files

It is common in AngularJS applications to put the module and the controllers in JavaScript files.

In this example, "myApp.js" contains an application module definition, while "myCtrl.js" contains the controller:

AngularJS Example

< !DOCTYPE html >
< html >
< script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js" > < /script >
< body >

< div ng-app= "myApp" ng-controller= "myCtrl" >
{{ firstName + " " + lastName }}
< /div >

< script src= "myApp.js" > < /script >
< script src= "myCtrl.js" > < /script >

< /body >
< /html >

Try it Yourself »

myApp.js

var app = angular.module( "myApp", []);
NoteThe [] parameter in the module definition can be used to define dependent modules.

myCtrl.js

app.controller( "myCtrl", function($scope) {
    $scope.firstName = "John";
    $scope.lastName= "Doe";
});

Functions can Pollute the Global Namespace

Global functions should be avoided in JavaScript. They can easily be overwritten or destroyed by other scripts.

AngularJS modules reduces this problem, by keeping all functions local to the module.


When to Load the Library?

NoteIn all our examples, the AngularJS library is loaded in the head of the HTML document.

A common advise for HTML applications, is to place scripts at the very bottom of the <body> element.

But, in many AngularJS examples, you will see the library in the head of the document.

This is because calls to angular.module can only be compiled after the library has been loaded.

Another solution is to load the AngularJS library in the <body> element, before your own AngularJS scripts:

AngularJS Example

< !DOCTYPE html >
< html >
< body >

< div ng-app= "myApp" ng-controller= "myCtrl" >
{{ firstName + " " + lastName }}
< /div >

< script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js" > < /script >
< script >
var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope) {
    $scope.firstName = "John";
    $scope.lastName = "Doe";
});
< /script >

< /body >
< /html >

Try it Yourself »
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值