国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

AngularJS 視圖詳解及示例代碼

2019-11-20 09:13:25
字體:
來源:轉載
供稿:網友

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

ng-view

ng-view 標記只是簡單地創建一個占位符,是一個相應的視圖(HTML或ng-template視圖),可以根據配置來放置。

使用

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

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

ng-template

ng-template 指令是用來創建使用script標簽的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是組網址的配置,將它們映射相應的HTML頁面或 ng-template,并附加一個控制器使用相同鍵的服務。

使用

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

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

使用

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

 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被定義為使用關鍵字作為'$routeProvider“下mainApp模塊的配置功能;

$routeProvider當定義了URL“/addStudent”映射到“addStudent.html”。 addStudent.html應存在于相同的路徑主要的html 頁面。如果htm頁面沒有定義,那么ng-template被id=“addStudent.html”使用。我們已經使用了ng-template;

“otherwise”是用來設置的默認視圖;

“conlloer”是用來設置該視圖對應的控制器;

例子

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

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>

結果

在Web瀏覽器中打開textAngularJS.html。看到結果如下:

以上就是對AngularJS 視圖資料的整理,后續繼續補充相關資料,謝謝大家對本站的支持!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 南部县| 建昌县| 庄浪县| 闸北区| 县级市| 军事| 正定县| 偃师市| 邹平县| 万荣县| 英德市| 民勤县| 宜川县| 吐鲁番市| 鹿泉市| 会东县| 阳江市| 宜章县| 武陟县| 兴隆县| 肇东市| 同德县| 太和县| 泾源县| 监利县| 青海省| 东兰县| 定西市| 安义县| 涟源市| 襄城县| 辽阳市| 惠东县| 随州市| 合山市| 阿荣旗| 龙井市| 双流县| 夏河县| 普格县| 河源市|