12

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>.
0

2 Answers 2

5

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

Sign up to request clarification or add additional context in comments.

3 Comments

When I try <a ng-href="/foo">foo</a> I get a full page reload. Am I doing something wrong?
@sammla, Have you checked the example of $location?
Yes, and I see its working there, but I can't figure out what configuration I am missing. I tried adding a base href with no luck. And the example says "Here you can see two $location instances, both in Html5 mode" and in the script I see $locationProvider.html5Mode(true).hashPrefix('!'); (I am using hashbang mode), but in the example it uses hashbang. It's kind of confusing.. :S
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.