angularJS系列之监听路由变化$location和$route实例

直接上代码

  //增加路由跳转时的判断,如果是同一个页面重新刷新,则让其跳转到相应的页面。
        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-----结束 ');
            }
        }]);
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值