使用AngularJS时,当路由发生改变时,我们需要做某些处理,此时可以监听路由事件,常用的是$routeStartChange, $routeChangeSuccess。完整例子如下:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>AngularJS监听路由变化</title>
</head>
<body ng-app="ngRouteExample">
<div id="navigation">
<a href="#/home">Home</a>
<a href="#/about">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) {
/* 监听路由的状态变化 */
$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的,对于Angular2是否也可以用,还没尝试过,有机会验证了再记录下咯~~

AngularJS监听路由变化的更多相关文章

  1. 【转载】AngularJS监听路由变化

    一.Angular 路由状态发生改变时可以通过' $stateChangeStart '.' $stateChangeSuccess '.' $stateChangeError '监听,通过注入'$l ...

  2. Angular 监听路由变化

    var app = angular.module('Mywind',['ui.router']) //Angular 监听路由变化 function run($ionicPlatform, $loca ...

  3. vue 如何通过监听路由变化给父级路由菜单添加active样式

    1.项目需求:在项目开发中,多级菜单的情况下,勾选子菜单时,需要在父级菜单添加active样式. 2.遇到的问题:一级路由菜单的话,点击当前路由会自动在路由标签上添加router-link-exact ...

  4. mint ui的tabBar监听路由变化实现tabBar切换

    说明 最近学习vue,使用了mint ui的tabBar,感觉好难受,结合 tab-container使用更难受,因为它不是根据路由来切换页面的.mui与它基本相反,因此它能根据搜索栏的路由变化,相应 ...

  5. angular 全局 监听路由变化

    app.run(['$rootScope', '$location', function($rootScope, $location) { /* 监听路由的状态变化 */ $rootScope.$on ...

  6. Angular 监听路由变化事件

    摘要: $stateChangeStart- 当模板开始解析之前触发 $rootScope.$on('$stateChangeStart', function(event, toState, toPa ...

  7. vue 监听路由变化

    方法一:通过 watch // 监听,当路由发生变化的时候执行 watch:{ $route(to,from){ console.log(to.path); } }, 或 // 监听,当路由发生变化的 ...

  8. AngularJS监听数组变化

    我们在使用angualr的监听时候,业务的需要我们会去监听一个数组的某一个值得变化,再写逻辑代码.然而我们在使用$scope.$watch("",function(){ })时候会 ...

  9. vue监听路由变化

    使用 watch,观察路由,一旦发生变化便重新获取数据 watch: { // 如果路由有变化,会再次执行该方法 '$route': 'fetchData' }

随机推荐

  1. phpstorm中FTP自动同步功能

    首先打开PhpStorm软件,新建个项目完成以后,找到Tools,  找到 Tools->Deployment->configruation点击进行设置,  点击configruation ...

  2. ab压力测试遭遇apr_socket_recv: Connection reset by peer (104) 怎么办

    ab -r  -c 2000 -n 200000  www.baidu.com 其实只要加上-r就可以了.但是为什么呢?ab --help就知道了 当Socket接收到错误的时候不退出,就是这句.事实 ...

  3. AWS邮件通知服务:实时监控邮件状态

    以下为译文: 订阅“AWS中文技术社区”微信公众号,实时掌握AWS技术及产品消息! AWS中文技术社区为广大开发者提供了一个Amazon Web Service技术交流平台,推送AWS最新资讯.技术视 ...

  4. numpy的prod()函数和pad()函数

    1.np.prod()函数用来计算所有元素的乘积,对于有多个维度的数组可以指定轴,如axis=1指定计算每一行的乘积. 2.np.lib.pad()函数用来把原来的list在原来的维度上进行扩展 例1 ...

  5. Jmeter接口测试自动化 (三)(数据驱动测试)

    本文转载至http://www.cnblogs.com/chengtch/p/6105532.html 当我研究要通过用例优先级控制用例是否执行时,我发现了用"如果(if)控制器" ...

  6. json 的相互 转换

    using System.Runtime.Serialization.Json; //json 转化为List集合 public List<T> JSONStringToList<T ...

  7. windows脚本的if语句

  8. 27. Remove Element(双指针)

      Given an array nums and a value val, remove all instances of that value in-place and return the ne ...

  9. MATLAB 简明教程

    MATAB 是我学习和接触的第一种工具类的编程语言,最早可以追溯到大一上数学分析这门课的时候.MATLAB既是一种软件也是一门编程语言,MATLAB功能强大在理科和工科中运用较多. MATLAB 是 ...

  10. python 文件操作,os.path.walk()的回调函数打印文件名

    #coding=utf-8 import osdef find_file(arg,dirname,files):    #for i in arg:        #print i for file ...