删除页面上的$位置搜索刷新/重新加载AngularJS



如果用户重新加载页面或刷新它,则需要从URL中清除状态标志(搜索参数(。我正在使用angularjs 1.5.8。

我可以检测重新加载/刷新事件,甚至可以输出到控制台,但是删除搜索参数的代码不会发射。这是我正在使用的

var windowElement = angular.element($window);
windowElement.on('beforeunload', function (event) {
    $location.url($location.path());
    console.info("page refreshed." + $location.path());        
});

任何指针?

尝试

$location.search({});

删除所有参数。如果要删除特定的,则

$location.search('key', null);

应该做这个问题。

另外,请查看$ destrot Destracter lister,该听众告诉Angular,在$ destroy of Controller/Diactive时,请致电此功能。

$scope.$on('$destroy', function () {
   $location.search({});
});

文档中没有太多...但是在这里。

希望这对他人有所帮助,因为我在如此工作中看到的建议都没有。我设法创建了一种黑客,以使用以下代码在 app.run function中进行此操作。

$rootScope.$on('$stateChangeStart', function (event, toState, toParams, fromState, fromParams) {
    //not coming from any other page + coming to ads page + contains query string
    if (fromState.name == "" && toState.name == "current_page" && toState.url !== "/current-page-url") {
        $location.search({});
    }
});

它的作用是,如果我从其他页面来到页面,它将具有源自源的填充,但是当我重新加载当前页面时,从省将没有数据,而tostate则将在当前页面的详细信息。为避免连续循环,我还检查当前URL是否包含任何问题。

更新甚至更好的解决方案。从此链接找到了一些帮助。

$rootScope.$on("$locationChangeStart", function (event, next, current) {
        //Page reload detection
        if (next == current && && performance.navigation.type == 1) {
            event.preventDefault();
            $location.search({});
        }
    });

第一个解决方案即使第一次加载带有查询字符串的页面(通过一些书签(即使查询字符串。

第二个解决方案按预期工作。

请参阅此参考:https://developer.mozilla.org/en-us/docs/web/api/window/performance/performance

最新更新