0

What is the right way to define links to angular routes in hashbang (non-HTML5) mode?

Suppose that I have the following nav. bar:

  <div class="collapse navbar-collapse">
    <ul class="nav navbar-nav">
      <li><a href="#!/first">First</a></li>
      <li><a href="#!/second">Second</a></li>
    </ul>
    <ul class="nav navbar-nav navbar-right">
      <li><a href="#!/" ng-click="logout()">Logout</a></li>
    </ul>
  </div>

It works but I have the following questions in mind:

  • I saw a lot of articles where authors do the same thing via /first or #/first instead of #!/first but it doesn't work for me for some reason (I guess that's because I'm using #! as a hashstring but I don't know why there's so many examples with #/first instead of #!/first if the latter is the default one)
  • Is it ok to define logout action this way?

    <li><a href="#!/" ng-click="logout()">Logout</a></li>
    

Because it seems a little bit ugly for me.

Mistalis
  • 17,793
  • 13
  • 73
  • 97
FrozenHeart
  • 19,844
  • 33
  • 126
  • 242
  • angular routes were changed in 1.6. see http://stackoverflow.com/questions/41211875/angularjs-1-6-0-latest-now-routes-not-working – Claies Feb 24 '17 at 11:06

1 Answers1

1

The hash-bang synthax is new from Angular 1.6. This release is still new, it may explain why you don't see many articles with the hash-bang routing.

Anyway, from this line:

<li><a href="#!/" ng-click="logout()">Logout</a></li>

You can move the redirection to the logout() function:

$scope.logout = function() {
    ...
    $location.url('/home');
}

Don't forget to inject $location.

Community
  • 1
  • 1
Mistalis
  • 17,793
  • 13
  • 73
  • 97