
- 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 透過單頁面上的多個檢視支援單頁面應用程式。為此,AngularJS 提供了 ng-view 和 ng-template 指令以及 $routeProvider 服務。
ng-view 指令
ng-view 指令簡單地建立一個佔位符,根據配置,可以在其中放置相應的檢視(HTML 或 ng-template 檢視)。
用法
在主模組中定義一個帶有 ng-view 的 div。
<div ng-app = "mainApp"> ... <div ng-view></div> </div>
ng-template 指令
ng-template 指令用於使用 script 標籤建立 HTML 檢視。它包含一個 id 屬性,該屬性由 $routeProvider 用於將檢視與控制器對映。
用法
在主模組中定義一個型別為 ng-template 的 script 塊。
<div ng-app = "mainApp"> ... <script type = "text/ng-template" id = "addStudent.htm"> <h2> Add Student </h2> {{message}} </script> </div>
$routeProvider 服務
$routeProvider 是一個關鍵服務,它設定 URL 的配置,將它們與相應的 HTML 頁面或 ng-template 對映,並附加相同的控制器。
用法 1
在主模組中定義一個型別為 ng-template 的 script 塊。
<div ng-app = "mainApp"> ... <script type = "text/ng-template" id = "addStudent.htm"> <h2> Add Student </h2> {{message}} </script> </div>
用法 2
定義一個帶有主模組的 script 塊並設定路由配置。
var mainApp = angular.module("mainApp", ['ngRoute']); mainApp.config(['$routeProvider', function($routeProvider) { $routeProvider .when('/addStudent', { templateUrl: 'addStudent.htm', controller: 'AddStudentController' }) .when('/viewStudents', { templateUrl: 'viewStudents.htm', controller: 'ViewStudentsController' }) .otherwise ({ redirectTo: '/addStudent' }); }]);
在上面的例子中,需要考慮以下幾點:
$routeProvider 在使用鍵“$routeProvider”的 mainApp 模組的 config 中定義為一個函式。
$routeProvider.when 定義了一個 URL "/addStudent",它對映到 "addStudent.htm"。addStudent.htm 應該與主 HTML 頁面位於相同的路徑。如果未定義 HTML 頁面,則需要使用 id="addStudent.htm" 的 ng-template。我們使用了 ng-template。
"otherwise" 用於設定預設檢視。
"controller" 用於為檢視設定相應的控制器。
示例
以下示例展示了所有上述指令的用法。
testAngularJS.htm
<html> <head> <title>Angular JS Views</title> <script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script> <script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular-route.min.js"> </script> </head> <body> <h2>AngularJS Sample Application</h2> <div ng-app = "mainApp"> <p><a href = "#addStudent">Add Student</a></p> <p><a href = "#viewStudents">View Students</a></p> <div ng-view></div> <script type = "text/ng-template" id = "addStudent.htm"> <h2> Add Student </h2> {{message}} </script> <script type = "text/ng-template" id = "viewStudents.htm"> <h2> View Students </h2> {{message}} </script> </div> <script> var mainApp = angular.module("mainApp", ['ngRoute']); mainApp.config(['$routeProvider', function($routeProvider) { $routeProvider .when('/addStudent', { templateUrl: 'addStudent.htm', controller: 'AddStudentController' }) .when('/viewStudents', { templateUrl: 'viewStudents.htm', controller: 'ViewStudentsController' }) .otherwise({ redirectTo: '/addStudent' }); }]); mainApp.controller('AddStudentController', function($scope) { $scope.message = "This page will be used to display add student form"; }); mainApp.controller('ViewStudentsController', function($scope) { $scope.message = "This page will be used to display all the students"; }); </script> </body> </html>
輸出
在 Web 瀏覽器中開啟檔案 testAngularJS.htm 並檢視結果。