【问题标题】:AngularJS routeProvider not adding hash at the end of URLAngularJS routeProvider不在URL末尾添加哈希
【发布时间】:2013-04-10 17:28:30
【问题描述】:

我配置了以下 routeProvider:

 angular.module('myModule').config ['$routeProvider', (provider) ->

   provider
  .when '',
     templateUrl: '/templates/dashboard.html'

  .when '/some_path/:some_param',
       templateUrl: '/templates/dashboard.html'

在包装静态提供的模板中包含以下内容:

我也有适当定义的模板/dashboard.html。

当我导航到该 url 时,会加载初始页面,但是# 不会后挂在 URL 上,当我尝试使用 $location.path('fooPath') 在控制器中重写 URL 时会导致错误。

特别是 $location.path('fooPath'),将 URL 更改为 current_dashboard_path/#/ 并重新加载,而我所期望的是将 URL 设置为:

current_dashboard_path/#/,然后是location('fooPath'),将其更改为current_dashboard_path/#/fooPath

一些额外的上下文:我想使用它,这样我就可以使用$location 服务来更改 url 而无需重新加载页面

问题是,当填充 ng-view 时,如何强制 Angular 后置 #

【问题讨论】:

  • 你看过 $location.hash('value'); ?
  • $location.hash('value');将路径更改为 current_dashboard_path##value,而不是 current_dashboard_path/#/value 仍然没有通过正确的路径参数。

标签: angularjs routes coffeescript location-provider


【解决方案1】:

我的 routeProvider 中缺少以下行:

 .when '/',
      templateUrl: '/templates/dashboard.html'

另外,空白路线

 .when '',
     templateUrl: '/templates/dashboard.html'

需要删除

那么在不重新加载页面的情况下重写URL的方式在控制器中如下:

 lastRoute = route.current

 scope.$on('$locationChangeSuccess', (event)->
    route.current = lastRoute
 )

【讨论】:

    猜你喜欢
    • 2023-03-29
    • 1970-01-01
    • 1970-01-01
    • 2019-04-15
    • 2017-06-15
    • 1970-01-01
    • 1970-01-01
    • 2017-10-14
    • 1970-01-01
    相关资源
    最近更新 更多