preventdefault - $locationChangeStart 上的 PreventDefault() 停止每个重定向并且无法恢复
问题描述
目标:在不重新加载页面的情况下更改 URL,因为 URL 用于以后的功能,但无法轻松更改来自其他位置的重新链接。
环境:ServiceNow 实例(这就是为什么语法在某些时候对你们来说可能看起来有点奇怪。
$locationChangeStart
我需要在阻止它一次之后对事件处理进行反应。
知道如何恢复e.preventDefault()
功能吗?
代码:
var originalUrl, initUrl;
$rootScope.$on("$locationChangeStart", function(e, newUrl){
if(newUrl != originalUrl && newUrl != initUrl && c.count<c.maxCount ){
$window.history.replaceState(newUrl, $document.title, newUrl);
c.count++;
}
originalUrl = newUrl;
e.preventDefault();
$timeout(function(){
console.log("unbind")
//Both not working
$().unbind(preventDefault);
e.returnValue = true;
},2000)
});
$scope.changeURL = function(){
initUrl = $location.absUrl();
var params = $location.search();
params.o=$scope.data.sort_fields;
params.d=$scope.data.sort_direction;
params.initial_load= true;
//triggers the $locationChangeStart event
$location.search($httpParamSerializer(params));
}
$scope.changeURL()
解决方案
我不知道必须不断调用 e.preventDefault() 来停止事件。所以没有必要恢复它。只有在不应调用 e.preventDefault() 时才需要停止调用。
推荐阅读
- mysql - 如何获得大表的计数?
- python - 如何选择列并为不存在的列生成 Nan 值?
- vba - 如何在 Access 中将表单跳转到特定记录?
- c# - File.Open 抛出 IOException,但文件永远不会打开/显示
- gs-conditional-formatting - “如何根据 sam 列但前一行中的数据格式化单元格”
- powershell - 将 Microsoft CSV(历史搜索)导入 powershell 时遇到问题
- xml - Ansible:set_children 不使用变量
- asp.net-core-mvc - 如何覆盖 MVC Core 中的内置模板,类似于 MVC 4
- plugins - 我正在使用旅行 wp 优惠券代码问题
- node.js - 弹性搜索索引数据中的多个字段