本文實(shí)例講述了AngularJS實(shí)現(xiàn)使用路由切換視圖的方法。分享給大家供大家參考,具體如下:
目前創(chuàng)新互聯(lián)已為上1000家的企業(yè)提供了網(wǎng)站建設(shè)、域名、虛擬空間、網(wǎng)站托管運(yùn)營(yíng)、企業(yè)網(wǎng)站設(shè)計(jì)、四川網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長(zhǎng),共同發(fā)展。
下面是一個(gè)簡(jiǎn)單的學(xué)生信息管理實(shí)例。
注意:除了引用angular.js之外,還要引用angular-route.js。
1、創(chuàng)建index.html主視圖
在index.html主視圖中,我們將會(huì)把多個(gè)視圖共有的東西都放在里面,例如菜單。在這個(gè)例子中,我們僅僅把應(yīng)用的標(biāo)題放在里面,然后再用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>學(xué)生信息</title> <script src="/Scripts/angular.min.js"></script> <script src="/Scripts/angular-route.min.js"></script> <script src="controllers.js"></script> </head> <body> <h2>學(xué)生信息</h2> <div ng-view></div> </body> </html>
2、創(chuàng)建list.html列表視圖
<table> <tr> <th>學(xué)號(hào)</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詳細(xì)視圖
<div> <div><strong>學(xué)號(hào):</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、模板和控制器之前建立映射關(guān)系 function StuRouteConfig($routeProvider) { $routeProvider.when('/', { controller: 'ListController', templateUrl: 'list.html' }).when('/view/:id', { controller: 'DetailController', templateUrl: 'detail.html' }).otherwise({ redirectTo: '/' }); } //配置路由,以便學(xué)生服務(wù)能夠找到它 StuServices.config(StuRouteConfig); //一些虛擬的學(xué)生信息 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; }) //詳細(xì)模塊:從路由信息(從URL中解析出來的)中獲取郵件id,然后用它找到正確的郵件對(duì)象 StuServices.controller("DetailController", function ($scope, $routeParams) { $scope.Student = StudentList[$routeParams.id]; })
更多關(guān)于AngularJS相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《AngularJS入門與進(jìn)階教程》及《AngularJS MVC架構(gòu)總結(jié)》
希望本文所述對(duì)大家AngularJS程序設(shè)計(jì)有所幫助。
名稱欄目:AngularJS實(shí)現(xiàn)使用路由切換視圖的方法
當(dāng)前鏈接:http://muchs.cn/article36/jpgipg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站制作、定制網(wǎng)站、Google、App開發(fā)、企業(yè)建站、云服務(wù)器
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)