欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

AngularJS 視圖詳解及示例代碼

 更新時(shí)間:2016年08月17日 10:13:28   作者:李嘉圖  
本文主要介紹AngularJS 視圖,這里整理了相關(guān)知識(shí),并附代碼示例和實(shí)現(xiàn)效果圖,有興趣的小伙伴可以參考下

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

ng-view

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

使用

定義一個(gè)div與ng-view在主模塊中。

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

</div>  

ng-template

ng-template 指令是用來創(chuàng)建使用script標(biāo)簽的HTML視圖。它包含一個(gè)用于由$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,并附加一個(gè)控制器使用相同鍵的服務(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è)置該視圖對(duì)應(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é)果如下:

以上就是對(duì)AngularJS 視圖資料的整理,后續(xù)繼續(xù)補(bǔ)充相關(guān)資料,謝謝大家對(duì)本站的支持!

相關(guān)文章

  • 在Angular中使用Renderer2的操作代碼

    在Angular中使用Renderer2的操作代碼

    Renderer2 類是 Angular 提供的一個(gè)抽象服務(wù),允許在不直接操作 DOM 的情況下操縱應(yīng)用程序的元素,本文給大家介紹了如何在 Angular 中使用 Renderer2,文中通過代碼示例給大家講解的非常詳細(xì),需要的朋友可以參考下
    2024-03-03
  • cnpm加速Angular項(xiàng)目創(chuàng)建的方法

    cnpm加速Angular項(xiàng)目創(chuàng)建的方法

    這篇文章主要介紹了cnpm加速Angular項(xiàng)目創(chuàng)建的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-09-09
  • 詳解angularJs中關(guān)于ng-class的三種使用方式說明

    詳解angularJs中關(guān)于ng-class的三種使用方式說明

    本篇文章主要介紹了angularJs中關(guān)于ng-class的三種使用方式說明,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • Angular 實(shí)現(xiàn)輸入框中顯示文章標(biāo)簽的實(shí)例代碼

    Angular 實(shí)現(xiàn)輸入框中顯示文章標(biāo)簽的實(shí)例代碼

    這篇文章主要介紹了Angular 實(shí)現(xiàn)輸入框中顯示文章標(biāo)簽的實(shí)例代碼,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2018-11-11
  • Angular2中select用法之設(shè)置默認(rèn)值與事件詳解

    Angular2中select用法之設(shè)置默認(rèn)值與事件詳解

    在Angular.JS中可以使用數(shù)組或?qū)ο髣?chuàng)建一個(gè)下拉列表選項(xiàng)。關(guān)于Angular.js中select的基礎(chǔ)相信大家應(yīng)該都已經(jīng)了解了,那么下面這篇文章主要給大家介紹了Angular2中select用法之設(shè)置默認(rèn)值與事件的相關(guān)資料,需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-05-05
  • AngularJS ng-controller 指令簡(jiǎn)單實(shí)例

    AngularJS ng-controller 指令簡(jiǎn)單實(shí)例

    本文主要介紹AngularJS ng-controller 指令,這里對(duì)ng-controller指令資料的整理,并附代碼示例和效果圖,有需要的朋友看下
    2016-08-08
  • angular.js實(shí)現(xiàn)購物車功能

    angular.js實(shí)現(xiàn)購物車功能

    這篇文章主要為大家詳細(xì)介紹了angular.js購物車功能的實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-10-10
  • 通過AngularJS實(shí)現(xiàn)圖片上傳及縮略圖展示示例

    通過AngularJS實(shí)現(xiàn)圖片上傳及縮略圖展示示例

    本篇文章主要介紹了通過AngularJS實(shí)現(xiàn)圖片上傳及縮略圖展示,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-01-01
  • angular.js指令中transclude選項(xiàng)及ng-transclude指令詳解

    angular.js指令中transclude選項(xiàng)及ng-transclude指令詳解

    這篇文章主要研究一下如何使用ng-transclude指令,以及指令的transclude選項(xiàng)的相關(guān)資料,文中介紹的非常詳細(xì),并給出了完整的示例代碼大家參考學(xué)習(xí),需要的朋友們下面來一起看看吧。
    2017-05-05
  • AngularJS入門教程之多視圖切換用法示例

    AngularJS入門教程之多視圖切換用法示例

    這篇文章主要介紹了AngularJS多視圖切換用法,結(jié)合實(shí)例形式分析了AngularJS基于多視圖切換實(shí)現(xiàn)碎片文件動(dòng)態(tài)載入的相關(guān)操作技巧,需要的朋友可以參考下
    2016-11-11

最新評(píng)論