您的位置:首页 > Web前端 > AngularJS

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 函数 实例