舉例講解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)通過了一個(gè)空數(shù)組給它。此數(shù)組通常包含從屬模塊。
控制器模塊
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;
}
};
});
在這里,我們已經(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頁面導(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 模塊詳解及簡(jiǎn)單實(shí)例
- 淺析angularJS中的ui-router和ng-grid模塊
- AngularJS模塊學(xué)習(xí)之Anchor Scroll
- AngularJs動(dòng)態(tài)加載模塊和依賴注入詳解
- 深入淺析AngularJS中的module(模塊)
- 詳解AngularJS中module模塊的導(dǎo)入導(dǎo)出
- angularjs學(xué)習(xí)筆記之三大模塊(modal,controller,view)
- AngularJS模塊管理問題的非常規(guī)處理方法
- AngularJS中的模塊詳解
- AngularJS模塊詳解及示例代碼
相關(guān)文章
Agularjs妙用雙向數(shù)據(jù)綁定實(shí)現(xiàn)手風(fēng)琴效果
最近在工作總遇到需要實(shí)現(xiàn)類似手風(fēng)琴效果的需求,下面小編通過本文給大家分享angularjs巧用雙向數(shù)據(jù)綁定實(shí)現(xiàn)手風(fēng)琴效果,需要的朋友可以參考下2017-05-05
AngularJS 實(shí)現(xiàn)購物車全選反選功能
這篇文章主要介紹了AngularJS 實(shí)現(xiàn)購物車全選反選功能,需要的朋友可以參考下2017-10-10
Angular 4.x+Ionic3踩坑之Ionic3.x pop反向傳值詳解
這篇文章主要給大家介紹了關(guān)于Angular 4.x+Ionic3踩坑之Ionic3.x pop反向傳值的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2018-03-03
在 Angular6 中使用 HTTP 請(qǐng)求服務(wù)端數(shù)據(jù)的步驟詳解
本文分步驟給大家介紹了在 Angular6 中使用 HTTP 請(qǐng)求服務(wù)端數(shù)據(jù)的方法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-08-08
AngularJS實(shí)現(xiàn)controller控制器間共享數(shù)據(jù)的方法示例
這篇文章主要介紹了AngularJS實(shí)現(xiàn)controller控制器間共享數(shù)據(jù)的方法,結(jié)合簡(jiǎn)單實(shí)例形式分析了AngularJS控制器數(shù)據(jù)共享的實(shí)現(xiàn)方法,需要的朋友可以參考下2017-10-10
徹底學(xué)會(huì)Angular.js中的transclusion
這篇文章主要給大家介紹Angular.js中transclusion的相關(guān)資料,希望通過這一篇文章大家能夠弄懂Angular.js中的transclusion,文中介紹的很詳細(xì),需要的朋友可以參考借鑒,下面來一起看看吧。2017-03-03

