AngularJS 避繁就簡(jiǎn)的路由
AngularJS 路由允許我們通過(guò)不同的 URL 訪問(wèn)不同的內(nèi)容。
通過(guò) AngularJS 可以實(shí)現(xiàn)多視圖的單頁(yè)Web應(yīng)用(single page web application,SPA)。
通常我們的URL形式為 http://runoob.com/first/page,但在單頁(yè)Web應(yīng)用中 AngularJS 通過(guò) # + 標(biāo)記 實(shí)現(xiàn),例如:
http://runoob.com/#/first
http://runoob.com/#/second
http://runoob.com/#/third

先看看$routeProvider 的配置對(duì)象屬性方法:
路由設(shè)置對(duì)象解析:
$routeProvider.when(url(路由名稱(chēng)), {
template: string(模板提示字符串),
templateUrl: string(模板路徑URL),
controller: string, function 或 array(在當(dāng)前模板創(chuàng)建控制器,生成新的 $scope 作用域),
controllerAs: string(控制器別名),
redirectTo: string, function(重定向地址),
resolve: object<key, function>(當(dāng)前路由所依賴(lài)的模塊)
});
實(shí)現(xiàn)路由的大致步驟:
第一步:導(dǎo)入ngRoute模塊
<script type="text/javascript" src="js/angular-route.min.js"></script>
第二步:在應(yīng)用模塊中使用ngRoute
angular.module("routeApp", ["ngRoute"])
第三步:使用 ng-view 指令
<div ng-view class="well" ng-controller='defaultCtrl'></div>
第四步:配置 $routeProvider 路由規(guī)則
...
.config(['$routeProvider', function ($routeProvider){
$routeProvider
.when('/home', {
templateUrl : 'home.tpl.html',
controller : 'HomeCtrl',
})
.when('/computer', {
templateUrl : 'computer.html',
})
.when('/phone', {
templateUrl : 'phone.html',
})
.when('/other', {
templateUrl : 'other.tpl.html',
controller : 'OtherCtrl',
})
}])
...
第五步:通過(guò)超鏈接使用路由
<ul class="nav nav-tabs"> <li><a href="#/home">首頁(yè)</a></li> <li><a href="#/computer">電腦</a></li> <li><a href="#/phone">手機(jī)</a></li> <li><a href="#/other">其他</a></li> </ul>
完整案例:
1 route.html頁(yè)面
<html>
<head>
<meta charset="utf-8">
<title>AngularJS 路由實(shí)例</title>
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
</head>
<body ng-app='routeApp'>
<ul class="nav nav-tabs">
<li><a href="#/home">首頁(yè)</a></li>
<li><a href="#/computer">電腦</a></li>
<li><a href="#/phone">手機(jī)</a></li>
<li><a href="#/other">其他</a></li>
</ul>
<div ng-view class="well" ng-controller='defaultCtrl'></div>
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/bootstrap.min.js"></script>
<script type="text/javascript" src="js/angular.min.js"></script>
<script type="text/javascript" src="js/angular-route.min.js"></script>
<script type="text/ng-template" id="home.tpl.html">
<h1>{{data}}</h1>
</script>
<script type="text/ng-template" id="other.tpl.html">
<h1>{{data}}</h1>
</script>
<script type="text/javascript">
angular.module("routeApp", ["ngRoute"])
.config(['$routeProvider', function ($routeProvider){
$routeProvider
.when('/home', {
templateUrl : 'home.tpl.html',
controller : 'HomeCtrl',
})
.when('/computer', {
templateUrl : 'computer.html',
})
.when('/phone', {
templateUrl : 'phone.html',
})
.when('/other', {
templateUrl : 'other.tpl.html',
controller : 'OtherCtrl',
})
}])
.controller('defaultCtrl', function ($scope) {
$scope.computers = [
{ id: 0, name: "宏基", category: "Test", price: 1.25 },
{ id: 1, name: "聯(lián)想", category: "Test", price: 2.45 },
{ id: 2, name: "蘋(píng)果", category: "Test", price: 4.25 }
];
$scope.phones = [
{ id: 0, name: "三星", category: "Test", price: 1.25 },
{ id: 1, name: "榮耀", category: "Test", price: 2.45 },
{ id: 2, name: "魅族", category: "Test", price: 4.25 }
];
})
.controller("HomeCtrl", function ($scope, $route) {
$scope.$route = $route;
$scope.data = "Home Home";
})
.controller("OtherCtrl", function ($scope, $route) {
$scope.$route = $route;
$scope.data = "Other Other";
})
</script>
</body>
</html>
2.computer.html 頁(yè)面
<div class="panel-body">
<table class="table table-striped table-hover">
<thead>
<tr>
<th>名稱(chēng)</th>
<th>類(lèi)別</th>
<th class="text-right">價(jià)格</th>
<th>{{data}}</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="item in computers">
<td>{{item.name}}</td>
<td>{{item.category}}</td>
<td class="text-right">{{item.price | currency}}</td>
<td class="text-center">
<button class="btn btn-xs btn-primary" ng-click="deleteProduct(item)">刪除</button>
<a href="/edit/{{item.id}}" class="btn btn-xs btn-primary" ng-click="editOrCreateProduct(item)">編輯</a>
<button class="btn btn-xs btn-primary" ng-click="incrementPrice(item)">+</button>
</td>
</tr>
</tbody>
</table>
<div>
<button class="btn btn-xs btn-primary" ng-click="editOrCreateProduct()">添加</button>
<a href="create" class="btn btn-xs btn-primary" ng-click="editOrCreateProduct()">Add</a>
</div>
</div>
3.phone.html 頁(yè)面
<div class="panel-body">
<table class="table table-striped table-hover">
<thead>
<tr>
<th>名稱(chēng)</th>
<th>類(lèi)別</th>
<th class="text-right">價(jià)格</th>
<th>{{data}}</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="item in phones">
<td>{{item.name}}</td>
<td>{{item.category}}</td>
<td class="text-right">{{item.price | currency}}</td>
<td class="text-center">
<button class="btn btn-xs btn-primary" ng-click="deleteProduct(item)">刪除</button>
<a href="/edit/{{item.id}}" class="btn btn-xs btn-primary" ng-click="editOrCreateProduct(item)">編輯</a>
<button class="btn btn-xs btn-primary" ng-click="incrementPrice(item)">+</button>
</td>
</tr>
</tbody>
</table>
<div>
<button class="btn btn-xs btn-primary" ng-click="editOrCreateProduct()">添加</button>
<a href="create" class="btn btn-xs btn-primary" ng-click="editOrCreateProduct()">Add</a>
</div>
</div>
單擊“首頁(yè)”

單擊“電腦”

單擊“手機(jī)”

單擊“其他”

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 微信+angularJS的SPA應(yīng)用中用router進(jìn)行頁(yè)面跳轉(zhuǎn),jssdk校驗(yàn)失敗問(wèn)題解決
- Angularjs制作簡(jiǎn)單的路由功能demo
- AngularJS 路由詳解和簡(jiǎn)單實(shí)例
- 使用AngularJS對(duì)路由進(jìn)行安全性處理的方法
- AngularJS 路由和模板實(shí)例及路由地址簡(jiǎn)化方法(必看)
- AngularJS監(jiān)聽(tīng)路由的變化示例代碼
- AngularJS通過(guò)ng-route實(shí)現(xiàn)基本的路由功能實(shí)例詳解
- 簡(jiǎn)單講解AngularJS的Routing路由的定義與使用
- AngularJs ng-route路由詳解及實(shí)例代碼
- AngularJS路由實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn)實(shí)例
相關(guān)文章
angular2 ng2-file-upload上傳示例代碼
這篇文章主要介紹了angular2 ng2-file-upload上傳示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-08-08
Angular JS 生成動(dòng)態(tài)二維碼的方法
這篇文章主要介紹了Angular JS 生成動(dòng)態(tài)二維碼的方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-02-02
angular中實(shí)現(xiàn)控制器之間傳遞參數(shù)的方式
本篇文章主要介紹了angular中實(shí)現(xiàn)控制器之間傳遞參數(shù)的方式,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-04-04
AngularJS 限定$scope的范圍實(shí)例詳解
這篇文章主要介紹了AngularJS 限定$scope的范圍實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-06-06
詳解Angular.js指令中scope類(lèi)型的幾種特殊情況
AngularJs最重要也是最難理解的模塊之一就是它的指令(directive)了,自定義指令配置有很多個(gè)參數(shù),下面這篇文章主要介紹了關(guān)于Angular.js指令中scope類(lèi)型的幾種特殊情況,需要的朋友可以參考下。2017-02-02
Angular實(shí)現(xiàn)點(diǎn)擊按鈕控制隱藏和顯示功能示例
這篇文章主要介紹了Angular實(shí)現(xiàn)點(diǎn)擊按鈕控制隱藏和顯示功能,結(jié)合實(shí)例形式分析了AngularJS簡(jiǎn)單控制頁(yè)面元素顯示與隱藏的相關(guān)操作技巧,需要的朋友可以參考下2017-12-12
AngularJS 將再發(fā)布一個(gè)重要版本 然后進(jìn)入長(zhǎng)期支持階段
目前團(tuán)隊(duì)正在開(kāi)發(fā) AngularJS 1.7.0,而 1.7 的開(kāi)發(fā)周期將一直持續(xù)到 2018 年 6 月 30 日2018-01-01

