天天看點

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') >= ) {
                        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') >= ) {
                        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') >= ) {
//                        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-----結束 ');
            }
        }]);
           

繼續閱讀