
- AngularJS 教程
- AngularJS - 首頁
- AngularJS - 概述
- AngularJS - 環境搭建
- AngularJS - MVC 架構
- AngularJS - 第一個應用
- AngularJS - 指令
- AngularJS - 表示式
- AngularJS - 控制器
- AngularJS - 過濾器
- AngularJS - 表格
- AngularJS - HTML DOM
- AngularJS - 模組
- AngularJS - 表單
- AngularJS - 包含
- AngularJS - AJAX
- AngularJS - 檢視
- AngularJS - 作用域
- AngularJS - 服務
- AngularJS - 依賴注入
- AngularJS - 自定義指令
- AngularJS - 國際化
- AngularJS 應用
- AngularJS - 待辦事項應用
- AngularJS - 記事本應用
- AngularJS - Bootstrap 應用
- AngularJS - 登入應用
- AngularJS - 上傳檔案
- AngularJS - 內聯應用
- AngularJS - 導航選單
- AngularJS - 切換選單
- AngularJS - 訂單表單
- AngularJS - 搜尋標籤頁
- AngularJS - 拖拽應用
- AngularJS - 購物車應用
- AngularJS - 多語言應用
- AngularJS - 圖表應用
- AngularJS - 地圖應用
- AngularJS - 分享應用
- AngularJS - 天氣應用
- AngularJS - 計時器應用
- AngularJS - Leaflet 應用
- AngularJS - Lastfm 應用
- AngularJS 有用資源
- AngularJS - 問答
- AngularJS - 快速指南
- AngularJS - 有用資源
- AngularJS - 討論
AngularJS - 模組
AngularJS 支援模組化方法。模組用於將服務、控制器、應用等邏輯與程式碼分離,保持程式碼整潔。我們在單獨的 js 檔案中定義模組,並根據模組命名它們為 module.js 檔案。在下面的示例中,我們將建立兩個模組:
應用模組 - 用於使用控制器初始化應用。
控制器模組 - 用於定義控制器。
應用模組
這是一個名為 *mainApp.js* 的檔案,包含以下程式碼:
var mainApp = angular.module("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; } }; });
這裡,我們使用 mainApp.controller 函式宣告一個控制器 **studentController** 模組。
使用模組
<div ng-app = "mainApp" ng-controller = "studentController"> ... <script src = "mainApp.js"></script> <script src = "studentController.js"></script> </div>
這裡,我們使用 ng-app 指令使用應用模組,使用 ng-controller 指令使用控制器。我們在主 HTML 頁面中匯入 mainApp.js 和 studentController.js。
示例
以下示例顯示了上述所有模組的使用。
testAngularJS.htm
<html> <head> <title>Angular JS Modules</title> <script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script> <script src = "/angularjs/src/module/mainApp.js"></script> <script src = "/angularjs/src/module/studentController.js"></script> <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> </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* 檔案。檢視結果。
廣告