亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

簡介AngularJS的視圖功能應(yīng)用

 更新時間:2015年06月17日 10:46:23   投稿:goldensun  
這篇文章主要介紹了AngularJS的視圖功能應(yīng)用,包括ng-view和ng-template以及$routeProvider的使用,以及 $routeProvider 需要的朋友可以參考下

 AngularJS支持通過在單個頁面上的多個視圖的單頁應(yīng)用。要做到這一點AngularJS提供ng-view 和 ng-template指令,以及 $routeProvider 服務(wù)。
ng-view

ng-view 標(biāo)記只是簡單地創(chuàng)建一個占位符,是一個相應(yīng)的視圖(HTML或ng-template視圖),可以根據(jù)配置來放置。
使用

定義一個div與ng-view在主模塊中。

<div ng-app="mainApp">
...
  <div ng-view></div>

</div>  

ng-template

ng-template 指令是用來創(chuàng)建使用script標(biāo)簽的HTML視圖。它包含一個用于由$routeProvider映射控制器視圖“id”屬性。
使用

定義類型作為主模塊中 ng-template 的腳本塊。

<div ng-app="mainApp">
...
  <script type="text/ng-template" id="addStudent.html">
   <h2> Add Student </h2>
     {{message}}
  </script>

</div>  

$routeProvider

$routeProvider是組網(wǎng)址的配置,將它們映射相應(yīng)的HTML頁面或 ng-template,并附加一個控制器使用相同鍵的服務(wù)。
使用

定義類型作為主模塊中 ng-template 的腳本塊。

<div ng-app="mainApp">
...
  <script type="text/ng-template" id="addStudent.html">
   <h2> Add Student </h2>
     {{message}}
  </script>

</div>  

 

使用

定義主模塊的腳本塊,并設(shè)置路由配置。

 var mainApp = angular.module("mainApp", ['ngRoute']);
   
   mainApp.config(['$routeProvider',
     function($routeProvider) {
      $routeProvider.
        when('/addStudent', {
         templateUrl: 'addStudent.html',
         controller: 'AddStudentController'
        }).
        when('/viewStudents', {
         templateUrl: 'viewStudents.html',
         controller: 'ViewStudentsController'
        }).
        otherwise({
         redirectTo: '/addStudent'
        });
     }]);

   

以下是在上面的例子中需要考慮的重要問題

  •     $routeProvider被定義為使用關(guān)鍵字作為'$routeProvider“下mainApp模塊的配置功能;
  •     $routeProvider當(dāng)定義了URL“/addStudent”映射到“addStudent.html”。 addStudent.html應(yīng)存在于相同的路徑主要的html 頁面。如果htm頁面沒有定義,那么ng-template被id=“addStudent.html”使用。我們已經(jīng)使用了ng-template;
  •     “otherwise”是用來設(shè)置的默認(rèn)視圖;
  •     “conlloer”是用來設(shè)置該視圖對應(yīng)的控制器;

例子

下面的例子將展示上述所有指令。
testAngularJS.html

<html>
<head>
  <title>Angular JS Views</title>
  <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js"></script>
  <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.25/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.html">
     <h2> Add Student </h2>
     {{message}}
   </script>
   <script type="text/ng-template" id="viewStudents.html">
     <h2> View Students </h2>   
     {{message}}
   </script> 
  </div>

  <script>
   var mainApp = angular.module("mainApp", ['ngRoute']);
   
   mainApp.config(['$routeProvider',
     function($routeProvider) {
      $routeProvider.
        when('/addStudent', {
         templateUrl: 'addStudent.html',
         controller: 'AddStudentController'
        }).
        when('/viewStudents', {
         templateUrl: 'viewStudents.html',
         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>

結(jié)果

在Web瀏覽器中打開textAngularJS.html??吹浇Y(jié)果如下:

2015617104059841.jpg (560×429)

相關(guān)文章

最新評論