首页 > 解决方案 > $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()

标签: preventdefaultrevert

解决方案


我不知道必须不断调用 e.preventDefault() 来停止事件。所以没有必要恢复它。只有在不应调用 e.preventDefault() 时才需要停止调用。


推荐阅读