martinpaulucci
martinpaulucci

Reputation: 2402

Handling links from template in AngularJS

currently I'm handling links this way:

<a ng-click="goToLink('/foo')">foo</a>

$scope.goToLink = function(url) {
    $location.path(url)
}

because if I do

<a ng-href="/foo">foo</a> 

the page is of course reloaded(and getting 404), because it should be something like:

<a ng-href="#/foo">foo</a>

Is there a directive or something to define links that are 'html5 mode agnostic', so that I don't have to put the hashtag in every link?

Something like

<a ng-smart-href="/foo">foo</a>.

Upvotes: 12

Views: 8797

Answers (2)

Nathan
Nathan

Reputation: 1851

In HTML5 mode, will rewrite <a href="/foo">link to foo</a> to hash-bang mode if in a legacy browser:

See the documentation on this: Using $location, Html link rewriting.

Upvotes: 0

SunnyShah
SunnyShah

Reputation: 30467

Check ng-href, It is exactly what you are looking for. http://docs.angularjs.org/api/ng.directive:ngHref

Check "Relative links" example from http://docs.angularjs.org/guide/dev_guide.services.$location

Upvotes: 5

Related Questions