AngularJS監(jiān)聽(tīng)路由變化的方法
使用AngularJS時(shí),當(dāng)路由發(fā)生改變時(shí),我們需要做某些處理,此時(shí)可以監(jiān)聽(tīng)路由事件,常用的是$routeStartChange, $routeChangeSuccess。完整例子如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>AngularJS監(jiān)聽(tīng)路由變化</title>
</head>
<body ng-app="ngRouteExample">
<div id="navigation">
<a href="#/home" rel="external nofollow" >Home</a>
<a href="#/about" rel="external nofollow" >About</a>
</div>
<div ng-view></div>
<script type="text/ng-template" id="home.html">
<h1> Home </h1>
<table>
<tbody>
<tr ng-repeat="x in records" style="background:#abcdef;">
<td>{{x.Name}}</td>
<td>{{x.Country}}</td>
</tr>
</tbody>
</table>
</script>
<script type="text/ng-template" id="about.html">
<h1> About </h1>
<p>在輸入框中嘗試輸入:</p>
<p>姓名:<input type="text" ng-model="name"></p>
<p>你輸入的為: {{name}}</p>
</script>
<script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>
<script src="http://apps.bdimg.com/libs/angular-route/1.3.13/angular-route.js"></script>
<script type="text/javascript">
angular.module('ngRouteExample', ['ngRoute'])
.config(function ($routeProvider) {
$routeProvider.
when('/home', {
templateUrl: 'home.html',
controller: 'HomeController'
}).
when('/about', {
templateUrl: 'about.html',
controller: 'AboutController'
}).
otherwise({
redirectTo: '/home'
});
})
.run(['$rootScope', '$location', function($rootScope, $location) {
/* 監(jiān)聽(tīng)路由的狀態(tài)變化 */
$rootScope.$on('$routeChangeStart', function(evt, next, current){
console.log('route begin change');
});
$rootScope.$on('$routeChangeSuccess', function(evt, current, previous) {
console.log('route have already changed :'+$location.path());
});
}])
.controller('HomeController', function ($scope) {
$scope.records = [{
"Name" : "Alfreds Futterkiste",
"Country" : "Germany"
},{
"Name" : "Berglunds snabbköp",
"Country" : "Sweden"
},{
"Name" : "Centro comercial Moctezuma",
"Country" : "Mexico"
},{
"Name" : "Ernst Handel",
"Country" : "Austria"
}]
})
.controller('AboutController', function ($scope) {
$scope.name = '呵呵';
});
</script>
</body>
</html>
上述的例子是AngularJS 1的,對(duì)于Angular2是否也可以用,還沒(méi)嘗試過(guò),有機(jī)會(huì)驗(yàn)證了再記錄下咯~~
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Angularjs制作簡(jiǎn)單的路由功能demo
- AngularJS 路由詳解和簡(jiǎn)單實(shí)例
- AngularJS路由實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn)實(shí)例
- angular.js 路由及頁(yè)面?zhèn)鲄⑹纠?/a>
- 使用AngularJS對(duì)路由進(jìn)行安全性處理的方法
- AngularJS監(jiān)聽(tīng)路由的變化示例代碼
- AngularJS 路由和模板實(shí)例及路由地址簡(jiǎn)化方法(必看)
- 詳解Angular4中路由Router類的跳轉(zhuǎn)navigate
- angular.js之路由的選擇方法
- AngularJS中的路由使用及實(shí)現(xiàn)代碼
相關(guān)文章
淺談Angular7 項(xiàng)目開(kāi)發(fā)總結(jié)
這篇文章主要介紹了淺談Angular7 項(xiàng)目開(kāi)發(fā)總結(jié),本文在此做一次遇到問(wèn)題的總結(jié),以便知識(shí)的掌握,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-12-12
Angular中的結(jié)構(gòu)指令模式及使用詳解
這篇文章主要為大家介紹了Angular中的結(jié)構(gòu)指令模式及使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
Angular中ng-bind和ng-model的區(qū)別實(shí)例詳解
這篇文章主要介紹了Angular中ng-bind和ng-model的區(qū)別實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-04-04
angularJs中ng-model-options設(shè)置數(shù)據(jù)同步的方法
今天小編就為大家分享一篇angularJs中ng-model-options設(shè)置數(shù)據(jù)同步的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-09-09
Angular 多級(jí)路由實(shí)現(xiàn)登錄頁(yè)面跳轉(zhuǎn)(小白教程)
詳解Angular4中路由Router類的跳轉(zhuǎn)navigate
AngularJs 延時(shí)器、計(jì)時(shí)器實(shí)例代碼
詳解Angular CLI + Electron 開(kāi)發(fā)環(huán)境搭建

