AngularJS模塊詳解及示例代碼
AngularJS支持模塊化的方法。模塊用于單獨(dú)的邏輯表示服務(wù),控制器,應(yīng)用程序等,并保持代碼的整潔。我們?cè)趩为?dú)的js文件中定義的模塊,并將其命名為按照module.js文件形式。在這個(gè)例子中,我們要?jiǎng)?chuàng)建兩個(gè)模塊。
Application Module - 用于初始化控制器應(yīng)用程序
Controller Module - 用于定義控制器
應(yīng)用模塊
mainApp.js
var mainApp = angular.module("mainApp", []);
在這里,我們已經(jīng)聲明使用 angular.module 功能的應(yīng)用程序 mainApp 模塊。我們已經(jīng)通過(guò)了一個(gè)空數(shù)組給它。此數(shù)組通常包含從屬模塊。
控制器模塊
mainApp.controller("studentController", function($scope) {
$scope.student = {
firstName: "Mahesh",
lastName: "Parashar",
fees:500,
subjects:[
{name:'Physics',marks:70},
{name:'Chemistry',marks:80},
{name:'Math',marks:65},
{name:'English',marks:75},
{name:'Hindi',marks:67}
],
fullName: function() {
var studentObject;
studentObject = $scope.student;
return studentObject.firstName + " " + studentObject.lastName;
}
};
});
在這里,我們已經(jīng)聲明采用studentController模塊的mainApp.controller功能的控制器。
使用模塊
<div ng-app="mainApp" ng-controller="studentController"> .. <script src="mainApp.js"></script> <script src="studentController.js"></script>
在這里,我們使用 ng-app 指令和控制器采用ng-controller指令應(yīng)用模塊。我們已經(jīng)在主要的HTML頁(yè)面導(dǎo)入mainApp.js和studentController.js。
示例
下面的例子將展示上述所有模塊。
testAngularJS.htm
<html>
<head>
<title>Angular JS Modules</title>
<style>
table, th , td {
border: 1px solid grey;
border-collapse: collapse;
padding: 5px;
}
table tr:nth-child(odd) {
background-color: #f2f2f2;
}
table tr:nth-child(even) {
background-color: #ffffff;
}
</style>
</head>
<body>
<h2>AngularJS Sample Application</h2>
<div ng-app="mainApp" ng-controller="studentController">
<table border="0">
<tr><td>Enter first name:</td><td><input type="text" ng-model="student.firstName"></td></tr>
<tr><td>Enter last name: </td><td><input type="text" ng-model="student.lastName"></td></tr>
<tr><td>Name: </td><td>{{student.fullName()}}</td></tr>
<tr><td>Subject:</td><td>
<table>
<tr>
<th>Name</th>
<th>Marks</th>
</tr>
<tr ng-repeat="subject in student.subjects">
<td>{{ subject.name }}</td>
<td>{{ subject.marks }}</td>
</tr>
</table>
</td></tr>
</table>
</div>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js"></script>
<script src="mainApp.js"></script>
<script src="studentController.js"></script>
</body>
</html>
mainApp.js
var mainApp = angular.module("mainApp", []);
studentController.js
mainApp.controller("studentController", function($scope) {
$scope.student = {
firstName: "Mahesh",
lastName: "Parashar",
fees:500,
subjects:[
{name:'Physics',marks:70},
{name:'Chemistry',marks:80},
{name:'Math',marks:65},
{name:'English',marks:75},
{name:'Hindi',marks:67}
],
fullName: function() {
var studentObject;
studentObject = $scope.student;
return studentObject.firstName + " " + studentObject.lastName;
}
};
});
輸出
在Web瀏覽器打開textAngularJS.htm??吹浇Y(jié)果如下。

以上就是AngularJS模塊相關(guān)知識(shí)的資料整理,后續(xù)繼續(xù)補(bǔ)充相關(guān)知識(shí),謝謝大家對(duì)本站的支持!
相關(guān)文章
Ubuntu系統(tǒng)下Angularjs開發(fā)環(huán)境安裝
本文主要介紹 Ubuntu系統(tǒng)下Angularjs開發(fā)環(huán)境安裝,這里詳細(xì)介紹了安裝步驟和注意事項(xiàng),有在Ubuntu 環(huán)境下開發(fā)的朋友可以參考下2016-09-09
angular2中router路由跳轉(zhuǎn)navigate的使用與刷新頁(yè)面問(wèn)題詳解
這篇文章主要給大家介紹了angular2中router路由跳轉(zhuǎn)navigate的使用與刷新頁(yè)面問(wèn)題的相關(guān)資料,文中介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-05-05
Angularjs注入攔截器實(shí)現(xiàn)Loading效果
angularjs作為一個(gè)全ajax的框架,對(duì)于請(qǐng)求,如果頁(yè)面上不做任何操作的話,在結(jié)果反回來(lái)之前,頁(yè)面是沒有任何響應(yīng)的,不像普通的HTTP請(qǐng)求,會(huì)有進(jìn)度條之類2015-12-12
Angular-Ui-Router+ocLazyLoad動(dòng)態(tài)加載腳本示例
本篇文章主要介紹了Angular-Ui-Router+ocLazyLoad動(dòng)態(tài)加載腳本示例,可以提高加載速度,使用戶體驗(yàn)更好,有興趣的可以了解一下。2017-03-03
使用AngularJS創(chuàng)建單頁(yè)應(yīng)用的編程指引
這篇文章主要介紹了使用AngularJS創(chuàng)建單頁(yè)應(yīng)用的編程指引,AngularJS是一款高人氣的JavaScript庫(kù),需要的朋友可以參考下2015-06-06
AngularJS遞歸指令實(shí)現(xiàn)Tree View效果示例
這篇文章主要介紹了AngularJS遞歸指令實(shí)現(xiàn)Tree View效果,結(jié)合實(shí)例形式分析了AngularJS基于遞歸指令實(shí)現(xiàn)樹形結(jié)構(gòu)層次數(shù)據(jù)的相關(guān)操作步驟與注意事項(xiàng),需要的朋友可以參考下2016-11-11
Angular 輸入框?qū)崿F(xiàn)自定義驗(yàn)證功能
AngularJS 表單和控件可以驗(yàn)證輸入的數(shù)據(jù)。本文給大家介紹Angular 輸入框?qū)崿F(xiàn)自定義驗(yàn)證功能,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下2017-02-02

