【问题标题】:How to prevent Angular from turning URL hash into a path如何防止 Angular 将 URL 哈希转换为路径
【发布时间】:2014-05-23 16:10:57
【问题描述】:

我有一个带有许多链接的 Angular 应用程序。单击每个链接会显示一组特定的数据。我希望每个链接在 URL 中设置一个哈希值,例如 http://foo.com/bar#item1,并且我还希望在直接访问带有哈希值的 URL 时显示带有特定数据集的页面。

我希望通过读取/操作 $location.hash() 来做到这一点,但是一旦我将 $location 注入我的控制器,URL 中的 #item1 就会更改为 #/item1。有没有办法阻止 Angular 这样做,以及设置我所描述的最佳方式是什么?

【问题讨论】:

标签: angularjs angularjs-routing


【解决方案1】:

您可以在路由/bar/item1 中传递参数,使用$routeParams 捕获它,如果存在则生成$location.hash()

"controller"

var anchor = $routeParams.anchorBottom;
if(anchor){
  $location.hash(anchor);
  $anchorScroll();
}


"link to section"

$location.path('/heart/bottom');


"route"

.when('/heart/:anchorBottom',{

You could see this running here.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-11
    • 2015-11-26
    • 1970-01-01
    • 2019-08-20
    • 2019-10-27
    • 2019-05-23
    • 2015-01-24
    • 2014-07-31
    相关资源
    最近更新 更多