【问题标题】:How to manually modify history in angular.js如何在 angular.js 中手动修改历史记录
【发布时间】:2014-02-04 23:02:38
【问题描述】:

我正在尝试从 url 中删除查询字符串(带有邀请令牌)而不重定向页面。

所以示例 url 看起来像:

example.com/?invitation=fooo

我已经包含了 ng-router,并且我正在使用 hashbang(我不能在我们的项目中使用 html5 模式)作为路由,但是这个查询字符串是主 url 的一部分(在 # 之前),因为我想要服务器去看看。

在常规应用中,我只会使用历史 API:

history.replaceState(null, null, '/');

但在使用 Angular 的应用程序中会导致错误:

Error: [$rootScope:infdig] 10 $digest() iterations reached. Aborting!

我不能使用 $location.path,因为 AFAIK 它只允许修改 url 的角度可路由部分,即在我的情况下 # 之后的部分。 $location.search 甚至都看不到它。

你知道在不重定向页面的情况下使用 Angular 从 url 中删除查询字符串的方法吗?

【问题讨论】:

    标签: angularjs url angular-routing


    【解决方案1】:

    在 Angular 中,对 URL 的所有修改都应该通过 $location 服务进行。您可以通过使用空对象调用$location.search 方法来清除查询字符串:$location.search({})。另外,请查看有关如何使用$location 服务here 的指南

    【讨论】:

    • 就像我说的,这个查询字符串不在可路由部分,因为它在 hashbang 之前,我没有使用 html5mode。 $location.search 看不到它:/ 也不能修改它。
    • 什么是“可路由”部分? $location 完全能够更改像 http://www.foo.com?bar=23#baz 这样的 URL。
    • 当我处于 hashbang 模式时,示例 url 可能看起来像:www.example.com/?invitation=fooo#!/base/path?foo=bar#baz 可路由部分我的意思是在 #!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-18
    • 2012-06-24
    • 1970-01-01
    相关资源
    最近更新 更多