舉例講解AngularJS中的模塊
AngularJS支持模塊化的方法。模塊用于單獨的邏輯表示服務,控制器,應用程序等,并保持代碼的整潔。我們在單獨的js文件中定義的模塊,并將其命名為按照module.js文件形式。在這個例子中,我們要創(chuàng)建兩個模塊。
- Application Module - 用于初始化控制器應用程序
- Controller Module - 用于定義控制器
應用模塊
mainApp.js
var mainApp = angular.module("mainApp", []);
在這里,我們已經(jīng)聲明使用 angular.module 功能的應用程序 mainApp 模塊。我們已經(jīng)通過了一個空數(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指令應用模塊。我們已經(jīng)在主要的HTML頁面導入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。看到結果如下。

相關文章
angularjs+bootstrap實現(xiàn)自定義分頁的實例代碼
本篇文章主要介紹了angularjs+bootstrap實現(xiàn)自定義分頁的實例代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-06-06
angular6.0開發(fā)教程之如何安裝angular6.0框架
這篇文章主要介紹了angular6.0開發(fā)教程之如何安裝angular6.0框架,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-06-06
利用Ionic2 + angular4實現(xiàn)一個地區(qū)選擇組件
ionic是一個移動端開發(fā)框架,使用hybird技術,只要使用前端開發(fā)技術就可以開發(fā)出電腦端,安卓端和ios端的站點程序。下面這篇文章主要給大家介紹了關于利用Ionic2 + angular4實現(xiàn)一個地區(qū)選擇組件的相關資料,需要的朋友可以參考借鑒,下面來一起看看吧。2017-07-07
AngularJS解決ng-if中的ng-model值無效的問題
本篇文章主要介紹了AngularJS解決ng-if中的ng-model值無效的問題,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-06-06
AngularJS動態(tài)綁定ng-options的ng-model實例代碼
本篇文章主要介紹了AngularJS動態(tài)綁定ng-options的ng-model實例代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-06-06

