angularJS系列之监听路由变化$location和$route实例
2016-07-17 18:05
671 查看
直接上代码
//增加路由跳转时的判断,如果是同一个页面重新刷新,则让其跳转到相应的页面。 app.run(['$rootScope', '$window', '$location', '$log', function ($rootScope, $window, $location, $log) { var locationChangeStartOff = $rootScope.$on('$locationChangeStart', locationChangeStart); var locationChangeSuccessOff = $rootScope.$on('$locationChangeSuccess', locationChangeSuccess); var routeChangeStartOff = $rootScope.$on('$routeChangeStart', routeChangeStart); var isSecond = false; // function locationChangeStart(event, newUrl, currentUrl) { //调试用信息,测试无误后可删除 console.log('arguments = ', arguments); console.log('newUrl = ', newUrl); console.log('decode -> newUrl = ', decodeURIComponent(newUrl)); console.log('currentUrl = ', currentUrl); if (decodeURIComponent(newUrl) == currentUrl) { console.log('currentUrl.indexof = ', currentUrl.indexOf('upload_topic_image')); if (currentUrl.indexOf('upload_topic_image') >= 0) { if (isSecond) { console.log("$location.path('http://ctb.qingguo.com/weixinCt/main#/upload_topic_start')"); $location.path('http://ctb.qingguo.com/weixinCt/main#/upload_topic_start'); isSecond = false; } else { isSecond = true; console.log('isSecond = ', isSecond); } event.preventDefault(); return; } } console.log('判断结束 '); } function locationChangeSuccess(event, newUrl, currentUrl) { //调试用信息,测试无误后可删除 console.log('arguments = ', arguments); console.log('newUrl = ', newUrl); console.log('decode -> newUrl = ', decodeURIComponent(newUrl)); console.log('currentUrl = ', currentUrl); if (decodeURIComponent(newUrl) == currentUrl) { console.log('currentUrl.indexof = ', currentUrl.indexOf('upload_topic_image')); if (currentUrl.indexOf('upload_topic_image') >= 0) { if (isSecond) { console.log("$location.path('http://ctb.qingguo.com/weixinCt/main#/upload_topic_start')"); $location.path('http://ctb.qingguo.com/weixinCt/main#/upload_topic_start'); isSecond = false; } else { isSecond = true; console.log('isSecond = ', isSecond); } event.preventDefault(); return; } } console.log('判断结束 '); } function routeChangeStart(event, newUrl, currentUrl) { //调试用信息,测试无误后可删除 console.log('routeChangeStart-----开始 '); console.log('arguments = ', arguments); if(newUrl != undefined && currentUrl != undefined && newUrl.$$route != undefined && currentUrl.loadedTemplateUrl !=undefined) { console.log('newUrl = ', newUrl); console.log('newUrl.url = ', newUrl.$$route.templateUrl); console.log('currentUrl = ', currentUrl.loadedTemplateUrl); if (newUrl.$$route.templateUrl == currentUrl.loadedTemplateUrl) { console.log('currentUrl.indexof = ', currentUrl.loadedTemplateUrl.indexOf('upload_topic_image')); if (currentUrl.loadedTemplateUrl.indexOf('upload_topic_image') >= 0) { // if (isSecond) { console.log("$location.path('http://ctb.qingguo.com/weixinCt/main#/upload_topic_start')"); $location.path('http://ctb.qingguo.com/weixinCt/main#/upload_topic_start'); isSecond = false; // } else { // isSecond = true; // console.log('isSecond = ', isSecond); // } event.preventDefault(); return; } } } console.log('routeChangeStart-----结束 '); } }]);
相关文章推荐
- Angularjs 跨域请求
- Mootools 1.2教程 函数
- autoit InputBox 函数
- 文件遍历排序函数
- 交换机升级排障实例
- 关于C#中排序函数的总结
- Oracle 函数大全[字符串函数,数学函数,日期函数]第1/4页
- ASP下经常用的字符串等函数参考资料
- PostgreSQL教程(五):函数和操作符详解(1)
- DOS批处理 函数定义与用法
- asp Chr 函数 数字转字母的方法
- Lua中的函数精讲笔记
- Lua中的闭合函数、非全局函数与函数的尾调用详解
- Lua中调用C++函数示例
- Lua实现split函数
- Lua常用时间函数使用实例
- Lua函数与字符串处理简明总结
- Lua学习笔记之表和函数
- Lua中实现sleep函数功能的4种方法
- Lua函数用法研究