【问题标题】:AngularJS is putting forward slash / after hash tagAngularJS 提出斜线 / 在哈希标记之后
【发布时间】:2014-08-26 19:11:02
【问题描述】:

我正在尝试将 AngularJS $anchorScroll$location.hash 一起使用。但是,当我设置哈希时,AngularJS 会在它之后添加一个正斜杠 /。

例如,网址为:http://localhost:13060/Dashboard。当我不包含 AngularJS 库时,我可以点击链接,#contact,然后转到http://localhost:13060/Dashboard#contact

但是当我包含 AngularJS 并单击链接时,它会转到 http://localhost:13060/Dashboard#/contact,从而阻止 $anchorScroll 工作。

编辑 $anchorScroll 不起作用

起始 URL 是 http://localhost:13060/Category。 当我添加一个类别时,它应该转到http://localhost:13060/Category#/#id(其中 id 是新的 id)并向下滚动页面到它。 URL 正在正确更新,但 $anchorScroll 没有滚动。

 //jump to new category
 $location.path("");
 $location.hash(cat.ID);
 $anchorScroll();

【问题讨论】:

    标签: angularjs hashtag anchor-scroll


    【解决方案1】:

    除非您使用 html5mode,从角度路由中删除散列,否则您将有 2 个散列,一个用于角度路由,另一个用于锚点。

    http://localhost:13060/Dashboard#/#contact

    假设您将路由路径设置为 /profiles 并且锚点位于该视图中,则 url 看起来像:

    http://localhost:13060/Dashboard#/profiles#contact

    【讨论】:

    • 我现在的 URL 格式正确,但 $anchorScroll() 没有向下滚动到页面。
    • 老实说,我从未使用过锚滚动。 $location.hash() 是否输出您所期望的(第二个哈希)?
    • 是的,它会正确更新哈希(当我指定路径时),但是除了反映更改的 URL 浏览器栏之外什么都没有发生。
    • 我发现了这个问题。我在承诺的结果中有 $anchorScroll(即 categories.then),我更改了 $scope。我在 $scope 更改传播到视图之前调用了散列函数,因此散列不存在,因此它不会滚动。我在 $anchorScroll 中添加了一个超时来修复它。
    • @jth_92 很高兴看到一个 jsfiddle 或其他东西,我有同样的问题,但我不知道你添加 $location.path(""); $location.hash(cat.ID);
    猜你喜欢
    • 2011-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-07
    • 1970-01-01
    • 2015-06-30
    • 2018-04-09
    • 2011-06-27
    相关资源
    最近更新 更多