【问题标题】:PreventDefault() on $locationChangeStart stops every redirect and cannot be reverted$locationChangeStart 上的 PreventDefault() 停止每个重定向并且无法恢复
【发布时间】:2021-11-11 04:05:33
【问题描述】:

目标:在不重新加载页面的情况下更改 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()

【问题讨论】:

    标签: preventdefault revert


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2013-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多