AngularJs ng-route路由詳解及實(shí)例代碼
前提
首先需要在頁(yè)面引入angular和angular-route,注意要在angular-route之前引入angular
<script src="../../bower_components/angular/angular.js"></script>
<script src="../../bower_components/angular-route/angular-route.js"></script>
這主要是因?yàn)閍ngular-route.js需要傳入window.angular這個(gè)參數(shù),而這個(gè)參數(shù)只有在加載完angular才會(huì)出現(xiàn)。
(function(window, angular, undefined) {
'use strict';
...
ngRouteModule.directive('ngView', ngViewFactory);
...
})(window, window.angular);
下載可以去官網(wǎng)下載,或者使用bower進(jìn)行安裝。
講解
路由功能是由 routeProvider服務(wù) 和 ng-view 搭配實(shí)現(xiàn),ng-view相當(dāng)于提供了頁(yè)面模板的掛載點(diǎn),當(dāng)切換URL進(jìn)行跳轉(zhuǎn)時(shí),不同的頁(yè)面模板會(huì)放在ng-view所在的位置; 然后通過(guò) routeProvider 配置路由的映射。
一般主要通過(guò)兩個(gè)方法:
when():配置路徑和參數(shù);
otherwise:配置其他的路徑跳轉(zhuǎn),可以想成default。
when的第二個(gè)參數(shù):
controller:對(duì)應(yīng)路徑的控制器函數(shù),或者名稱(chēng)
controllerAs:給控制器起個(gè)別名
template:對(duì)應(yīng)路徑的頁(yè)面模板,會(huì)出現(xiàn)在ng-view處,比如"<div>xxxx</div>"
templateUrl:對(duì)應(yīng)模板的路徑,比如"src/xxx.html"
resolve:這個(gè)參數(shù)著重說(shuō)下,該屬性會(huì)以鍵值對(duì)對(duì)象的形式,給路由相關(guān)的控制器綁定服務(wù)或者值。然后把執(zhí)行的結(jié)果值或者對(duì)應(yīng)的服務(wù)引用,注入到控制器中。如果resolve中是一個(gè)promise對(duì)象,那么會(huì)等它執(zhí)行成功后,才注入到控制器中,此時(shí)控制器會(huì)等待resolve中的執(zhí)行結(jié)果。
詳細(xì)的例子,可以參考下面的樣例。
redirectTo:重定向地址
reloadOnSearch:設(shè)置是否在只有地址改變時(shí),才加載對(duì)應(yīng)的模板;search和params改變都不會(huì)加載模板
caseInsensitiveMatch:路徑區(qū)分大小寫(xiě)
路由有幾個(gè)常用的事件:
$routeChangeStart:這個(gè)事件會(huì)在路由跳轉(zhuǎn)前觸發(fā)
$routeChangeSuccess:這個(gè)事件在路由跳轉(zhuǎn)成功后觸發(fā)
$routeChangeError:這個(gè)事件在路由跳轉(zhuǎn)失敗后觸發(fā)
使用
在頁(yè)面中,寫(xiě)入U(xiǎn)RL跳轉(zhuǎn)的按鈕鏈接 以及 ng-view標(biāo)簽
<div ng-controller="myCtrl"> <ul> <li><a href="#/a">click a</a></li> <li><a href="#/b">click b</a></li> </ul> <ng-view></ng-view> <!-- <div ng-view ></div> --> </div>
其中,ng-view可以當(dāng)作元素或者標(biāo)簽等。
javascript中需要定義跳轉(zhuǎn)的相關(guān)配置
<script type="text/javascript">
angular.module("myApp",["ngRoute"])
.controller("aController",function($scope,$route){
$scope.hello = "hello,a!";
})
.controller("bController",function($scope){
$scope.hello = "hello,b!";
})
.controller("myCtrl",function($scope,$location){
$scope.$on("$viewContentLoaded",function(){
console.log("ng-view content loaded!");
});
$scope.$on("$routeChangeStart",function(event,next,current){
//event.preventDefault(); //cancel url change
console.log("route change start!");
});
})
.config(function($routeProvider, $locationProvider) {
$routeProvider
.when('/a', {
templateUrl: 'a.html',
controller: 'aController'
})
.when('/b', {
templateUrl: 'b.html',
controller: 'bController',
resolve: {
// I will cause a 3 second delay
delay: function($q, $timeout) {
var delay = $q.defer();
$timeout(delay.resolve, 3000);
return delay.promise;
}
}
})
.otherwise({
redirectTo: '/a'
});
});
</script>
上面的代碼中,/b路徑中的resolve關(guān)聯(lián)來(lái)一個(gè)延遲方法,這個(gè)方法返回的時(shí)Promise對(duì)象,而且3秒鐘后才會(huì)返回結(jié)果。因此b頁(yè)面,在3秒后才會(huì)加載成功。
額外還需要提供兩個(gè)html:
a.html:
<div ng-controller="aController" style="height:500px;width:100%;background-color:green;">{{hello}}</div>
以及b.html:
<div ng-controller="bController" style="height:2500px;width:100%;background-color:blue;">{{hello}}</div>
這樣,就可以實(shí)現(xiàn)路由的跳轉(zhuǎn)了。
全部的代碼可以參考:
<html ng-app="myApp">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="../../bower_components/angular/angular.js"></script>
<script src="../../bower_components/angular-route/angular-route.js"></script>
</head>
<body>
<div ng-controller="myCtrl">
<ul>
<li><a href="#/a">click a</a></li>
<li><a href="#/b">click b</a></li>
</ul>
<ng-view></ng-view>
<!-- <div ng-view ></div> -->
</div>
<script type="text/javascript">
angular.module("myApp",["ngRoute"])
.controller("aController",function($scope,$route){
$scope.hello = "hello,a!";
})
.controller("bController",function($scope){
$scope.hello = "hello,b!";
})
.controller("myCtrl",function($scope,$location){
$scope.$on("$viewContentLoaded",function(){
console.log("ng-view content loaded!");
});
$scope.$on("$routeChangeStart",function(event,next,current){
//event.preventDefault(); //cancel url change
console.log("route change start!");
});
})
.config(function($routeProvider, $locationProvider) {
$routeProvider
.when('/a', {
templateUrl: 'a.html',
controller: 'aController'
})
.when('/b', {
templateUrl: 'b.html',
controller: 'bController',
resolve: {
// I will cause a 1 second delay
delay: function($q, $timeout) {
var delay = $q.defer();
$timeout(delay.resolve, 3000);
return delay.promise;
}
}
})
.otherwise({
redirectTo: '/a'
});
});
</script>
</body>
</html>
以上就是對(duì)AngularJS ng-route路由的資料整理,后續(xù)繼續(xù)補(bǔ)充相關(guān)資料,謝謝大家對(duì)本站的支持!
- AngularJS實(shí)現(xiàn)使用路由切換視圖的方法
- 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入門(mén)教程之路由與多視圖詳解
- AngularJS入門(mén)教程之路由機(jī)制ngRoute實(shí)例分析
- AngularJS路由實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn)實(shí)例
- AngularJS實(shí)現(xiàn)用戶(hù)登錄狀態(tài)判斷的方法(Model添加攔截過(guò)濾器,路由增加限制)
- AngularJS路由切換實(shí)現(xiàn)方法分析
相關(guān)文章
Angular4學(xué)習(xí)筆記router的簡(jiǎn)單使用
本篇文章主要介紹了Angular4學(xué)習(xí)筆記router的簡(jiǎn)單使用,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-03-03
使用AngularJS實(shí)現(xiàn)可伸縮的頁(yè)面切換的方法
這篇文章主要介紹了使用AngularJS實(shí)現(xiàn)可伸縮的頁(yè)面切換的方法,AngularJS是一款熱門(mén)的JavaScript庫(kù),需要的朋友可以參考下2015-06-06
利用Jasmine對(duì)Angular進(jìn)行單元測(cè)試的方法詳解
單元測(cè)試是一種能夠幫助開(kāi)發(fā)者驗(yàn)證代碼中某一部分有效性的技術(shù)。下面這篇文章主要給大家介紹了關(guān)于利用Jasmine對(duì)Angular進(jìn)行單元測(cè)試的相關(guān)資料,文中介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-06-06
詳解AngularJS ui-sref的簡(jiǎn)單使用
本篇文章主要介紹了詳解AngularJS ui-sref的簡(jiǎn)單使用,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-04-04
解決angularjs中同步執(zhí)行http請(qǐng)求的方法
今天小編就為大家分享一篇解決angularjs中同步執(zhí)行http請(qǐng)求的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-08-08
Angularjs 根據(jù)一個(gè)select的值去設(shè)置另一個(gè)select的值方法
今天小編就為大家分享一篇Angularjs 根據(jù)一個(gè)select的值去設(shè)置另一個(gè)select的值方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-08-08
AngularJS的ng Http Request與response格式轉(zhuǎn)換方法
這篇文章主要介紹了AngularJS的ng Http Request與response格式轉(zhuǎn)換方法,結(jié)合實(shí)例形式分析了AngularJS實(shí)現(xiàn)Request與response格式轉(zhuǎn)換操作的相關(guān)設(shè)置與使用技巧,需要的朋友可以參考下2016-11-11

