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服务
The $routeProvider是一个关键服务,它设置URL的配置,将它们与相应的HTML页面或ng-template映射,并附加相同的控制器。
用法1
在主模块内定义一个类型为ng-template的脚本块。
<div ng-app = "mainApp"> 
   ... 
   <script type = "text/ng-template" id = "addStudent.htm"> 
      <h2> Add Student </h2> 
      {{message}} 
   </script>  
</div>
用法2
定义一个带有主模块的脚本块,并设置路由配置。
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被定义为mainApp模块config中的一个函数,使用$keyrouteProvider。
- 
$routeProvider.when定义了一个URL”/addStudent”,它映射到”addStudent.htm”。 addStudent.htm应该与主HTML页面位于同一路径上。如果没有定义HTML页面,那么需要使用ng-template和id=”addStudent.htm”。我们使用了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>
输出
在网络浏览器中打开文件 testAngularJS.htm 并查看结果。

 极客教程
极客教程