AngularJS module
AngularJS module
A module defines an application.
A module is a container for different parts of an application.
A module is a container for application controllers.
A controller usually belongs to a module.
Create a module
You can create a module using AngularJS'sangular.modulefunction:
<
div
ng-app=
"myApp"
...
<
/div
<
script
var
app = angular.module(
"
myApp
"
, []);
<
/script
The "myApp" parameter corresponds to the HTML element that executes the application.
You can now add controllers, directives, filters, etc., to AngularJS applications.
Add controller
You can useng-controllerDirectives to add controllers to the application:
AngularJS instance
<
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
Try it »
You can learn more about controllers in AngularJS controller.
Add directives
AngularJS provides many built-in directives that you can use to add functionality to your application.
For the complete directive content, please refer to AngularJS reference manual.
Additionally, you can use modules to add your own directives to your application:
AngularJS instance
<
div
ng-app=
"myApp"
runoob-directive
<
/div
<
script
var
app = angular.module(
"myApp"
, []);
app.directive(
"runoobDirective"
,
function
() {
return
{
template :
"I created it in the instruction constructor!"
};
});
<
/script
Try it »
You can learn more about directives in AngularJS directive.
Modules and controllers are included in JS files
Typically, AngularJS applications include modules and controllers in JavaScript files.
In the following examples, "myApp.js" contains the application module definition, and "myCtrl.js" contains the controller:
AngularJS instance
<
!DOCTYPE
html
<
html
<
script
src=
"http://apps.bdimg.com/libs/angular.js/1.4.6/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 »
myApp.js
var app = angular.module(
"myApp"
, []);
|
| In the module definition, the [] parameter is used to define the module's dependencies.
Square brackets [] indicate that the module has no dependencies; if it has dependencies, the names of the dependent modules will be written in the brackets. | | --- | --- |
myCtrl.js
app.controller(
"myCtrl"
, function($scope) {
$scope.firstName = "John";
$scope.lastName= "Doe";
});
Functions affect the global namespace
In JavaScript, global functions should be avoided because they can easily be overwritten by other script files.
AngularJS modules scope all functions under the module, avoiding this issue.
When to load libraries?
![]() |
In our instance, all AngularJS libraries are loaded in the head of the HTML document. |
|---|
For HTML applications, it is generally recommended to place all scripts at the very bottom of the