Search code examples
angularjsroutescoffeescriptlocation-provider

AngularJS routeProvider not adding hash at the end of URL


I have the following routeProvider configured:

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

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

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

And the following in a wrapping statically served template:

I also have a templates/dashboard.html appropriately defined.

When I navigate to the url, the initial page loads, however a # is not postpended on the URL, which then results in errors when I try to rewrite the URL in a controller with $location.path('fooPath').

Specifically $location.path('fooPath'), changes the URL to current_dashboard_path/#/ and reloads, while what I was expecting is for the URL to be set to:

current_dashboard_path/#/, an then location('fooPath'), to change that to current_dashboard_path/#/fooPath

Some additional context: I want to use this so that I can then use the $location service to change the url without reloading the page thus

Question is, how can I force Angular to postpend a # when an ng-view is populated.


Solution

  • I was missing the following line in my routeProvider:

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

    Additionally, the blank route

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

    Needed to be removed

    Then the way to rewrite the URL without reloading the page is the following in the controller:

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