javascript - 在 AngularJS 中删除页面刷新/重新加载时的 $location 搜索

标签 javascript angularjs

如果用户重新加载页面或刷新页面,我需要清除 Url 中的 stateParams(搜索参数)。我正在使用 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);

应该可以解决问题。

此外,请查看 $destroy 监听器,它告诉 Angular 在 Controller /指令的 $destroy 上调用此函数。

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

文档中没有太多内容...但它是 here .

关于javascript - 在 AngularJS 中删除页面刷新/重新加载时的 $location 搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56863161/

相关文章:

javascript - 错误 : Route. put() 需要回调函数,但得到了 [对象未定义]

javascript - 打印带参数的函数代码 JS

javascript - 在 map 上添加标记: AngularJS

javascript - 加载页面时如何使用 Angular 检查复选框?

javascript - 任何人都可以指导我使用 js 或 python 组合可视化吗?

angularjs - 如何解析 Angular 指令中的文件?

javascript - Angular $http.get 总是改变 JSON 中的顺序

javascript - View 中的数据未与 Controller 绑定(bind)

javascript - 在 JavaScript 中复制任意 n 维数组?

javascript - 向数组内的现有对象添加属性