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

AngularJS實現(xiàn)使用路由切換視圖的方法

 更新時間:2017年01月24日 10:03:37   作者:pan_junbiao  
這篇文章主要介紹了AngularJS實現(xiàn)使用路由切換視圖的方法,結合學生信息管理系統(tǒng)為例分析了使用controllers.js控制器來切換視圖的具體步驟與相關操作技巧,需要的朋友可以參考下

本文實例講述了AngularJS實現(xiàn)使用路由切換視圖的方法。分享給大家供大家參考,具體如下:

下面是一個簡單的學生信息管理實例。

注意:除了引用angular.js之外,還要引用angular-route.js。

1、創(chuàng)建index.html主視圖

在index.html主視圖中,我們將會把多個視圖共有的東西都放在里面,例如菜單。在這個例子中,我們僅僅把應用的標題放在里面,然后再用ng-view指令來告訴Angular把視圖顯示在哪兒。

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" ng-app="StuApp">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>學生信息</title>
  <script src="/Scripts/angular.min.js"></script>
  <script src="/Scripts/angular-route.min.js"></script>
  <script src="controllers.js"></script>
</head>
<body>
  <h1>學生信息</h1>
  <div ng-view></div>
</body>
</html>

2、創(chuàng)建list.html列表視圖

<table>
  <tr>
    <th>學號</th>
    <th>姓名</th>
    <th>性別</th>
    <th>年齡</th>
  </tr>
  <tr ng-repeat="student in StudentList">
    <td>{{student.id}}</td>
    <td><a ng-href="#/view/{{student.id}}">{{student.name}}</a></td>
    <td>{{student.sex}}</td>
    <td>{{student.age}}</td>
  </tr>
</table>

3、創(chuàng)建detail.html詳細視圖

<div>
  <div><strong>學號:</strong>{{Student.id}}</div>
  <div><strong>姓名:</strong>{{Student.name}}</div>
  <div><strong>性別:</strong>{{Student.sex}}</div>
  <div><strong>年齡:</strong>{{Student.age}}</div>
  <a href="#/">返回</a>
</div>

4、創(chuàng)建controllers.js控制器腳本

//創(chuàng)建模塊
var StuServices = angular.module("StuApp", ['ngRoute']);
//在URL、模板和控制器之前建立映射關系
function StuRouteConfig($routeProvider) {
  $routeProvider.when('/', {
    controller: 'ListController',
    templateUrl: 'list.html'
  }).when('/view/:id', {
    controller: 'DetailController',
    templateUrl: 'detail.html'
  }).otherwise({ redirectTo: '/' });
}
//配置路由,以便學生服務能夠找到它
StuServices.config(StuRouteConfig);
//一些虛擬的學生信息
StudentList = [{ id: 0, name: '張三', sex: '男', age: 18 },
  { id: 1, name: '李四', sex: '女', age: 15 },
  { id: 2, name: '王五', sex: '男', age: 16 }
];
//列表模板
StuServices.controller("ListController", function ($scope) {
  $scope.StudentList = StudentList;
})
//詳細模塊:從路由信息(從URL中解析出來的)中獲取郵件id,然后用它找到正確的郵件對象
StuServices.controller("DetailController", function ($scope, $routeParams) {
  $scope.Student = StudentList[$routeParams.id];
})

更多關于AngularJS相關內(nèi)容感興趣的讀者可查看本站專題:《AngularJS入門與進階教程》及《AngularJS MVC架構總結

希望本文所述對大家AngularJS程序設計有所幫助。

相關文章

最新評論