AngularJS监听路由变化
使用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监听路由变化的更多相关文章
- 【转载】AngularJS监听路由变化
一.Angular 路由状态发生改变时可以通过' $stateChangeStart '.' $stateChangeSuccess '.' $stateChangeError '监听,通过注入'$l ...
- Angular 监听路由变化
var app = angular.module('Mywind',['ui.router']) //Angular 监听路由变化 function run($ionicPlatform, $loca ...
- vue 如何通过监听路由变化给父级路由菜单添加active样式
1.项目需求:在项目开发中,多级菜单的情况下,勾选子菜单时,需要在父级菜单添加active样式. 2.遇到的问题:一级路由菜单的话,点击当前路由会自动在路由标签上添加router-link-exact ...
- mint ui的tabBar监听路由变化实现tabBar切换
说明 最近学习vue,使用了mint ui的tabBar,感觉好难受,结合 tab-container使用更难受,因为它不是根据路由来切换页面的.mui与它基本相反,因此它能根据搜索栏的路由变化,相应 ...
- angular 全局 监听路由变化
app.run(['$rootScope', '$location', function($rootScope, $location) { /* 监听路由的状态变化 */ $rootScope.$on ...
- Angular 监听路由变化事件
摘要: $stateChangeStart- 当模板开始解析之前触发 $rootScope.$on('$stateChangeStart', function(event, toState, toPa ...
- vue 监听路由变化
方法一:通过 watch // 监听,当路由发生变化的时候执行 watch:{ $route(to,from){ console.log(to.path); } }, 或 // 监听,当路由发生变化的 ...
- AngularJS监听数组变化
我们在使用angualr的监听时候,业务的需要我们会去监听一个数组的某一个值得变化,再写逻辑代码.然而我们在使用$scope.$watch("",function(){ })时候会 ...
- vue监听路由变化
使用 watch,观察路由,一旦发生变化便重新获取数据 watch: { // 如果路由有变化,会再次执行该方法 '$route': 'fetchData' }
随机推荐
- jquery.dataTables表格中的内容怎么设置让它不自动换行
在table中增加 style="white-space: nowrap;" ,这样会撑大td.会出现滚动条. 其他内容配置:每列宽度: "aoColumnDefs&qu ...
- matplotlib--设置线条颜色及形状
一.控制颜色 b--blue c--cyan(青色) g--green k--black m--magenta(紫红色) r--red w--white y--yellow 颜色有三种表示方法,可以用 ...
- MongoDB 工具助手类(.NET)
在开发过程中,需要用到MongoDB,本身MongoDB自己对类的封装就特别好了.为了更加符合我们平时的开发使用,我现在进行了一个简单的封装操作. 连接数据库类:MongoDBContext usin ...
- jquery.lazyload 使用
1.引用js <script src="jquery.js" type="text/javascript"></script> < ...
- Dapper Extensions中修改Dialect
如果是MySql数据库,则修改为:DapperExtensions.DapperExtensions.SqlDialect = new MySqlDialect(); DapperExtensions ...
- BinarySearch
今天看代码,看到这么一段,开始没有看明白,记录下来备忘 foreach (FinancialReport r3 in addAorList) { i ...
- redis和memcached相关
应该选择哪一种缓存机制 redis相较于memcached更加年轻,功能更加强大. 对小型静态数据进行缓存处理,最具代表性的例子就是HTML代码片段.使用memcached所消耗内存更少. 其他情况下 ...
- zw版【转发·台湾nvp系列Delphi例程】HALCON SmallestRectangle1
zw版[转发·台湾nvp系列Delphi例程]HALCON SmallestRectangle1 procedure TForm1.Button1Click(Sender: TObject);var ...
- MyBatis学习笔记(三)——优化MyBatis配置文件中的配置
转自孤傲苍狼的博客:http://www.cnblogs.com/xdp-gacl/p/4264301.html 一.连接数据库的配置单独放在一个properties文件中 之前,我们是直接将数据库的 ...
- Android :64位支持的说明
https://blog.csdn.net/u012400885/article/details/52923765 https://blog.csdn.net/ouyang_peng/article/ ...