11

I'm using angular-ui-router and have an issue with empty a tags, like href='#'. I'm using bootstrap, which makes extensive use of href='#' for dropdowns and such. The problem is if a user selects a dropdown item then the router interprets that as a state change, which in this case is to the home page.

Is there an easy way to stop this behavior without having to resort to changing all the href='#' to href=''.

6
  • Can't you just remove the href attribute completely? It works for me when I'm using bootstrap and angular. Commented Aug 28, 2014 at 15:28
  • Yeah, is that safe cross-browser? I guess so, but just wanted be sure. Commented Aug 28, 2014 at 15:40
  • An interesting side note, the basic angular router doesn't do this. Commented Aug 28, 2014 at 15:42
  • Or you can also do this : href="javascript: void(0);" Commented Aug 28, 2014 at 17:09
  • 1
    Take a look at angular-ui.github.io/bootstrap native angular directives and helpers for bootstrap. Replaces the jQuery pluggins provided my bootstrap Commented Aug 28, 2014 at 19:46

3 Answers 3

10

Just remove the href tag completely from your anchor tag. It's still a perfectly valid tag without it.

Or if you're currently using ui-sref in the anchor tag, you could actually use the href attribute instead to go to the route that the state is mapped to.

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

5 Comments

Thanks, this is what I did in the end.
Why did someone downvote my answer? Care to give an explanation?
In the Bootstrap collapse, we need to do something like this: <a data-toggle="collapse" data-parent="#accordion" href="#3">, how can it work without the href?
@ElgsQianChen, in that case, I would use the collapse or accordion directives that come with Angular-UI: angular-ui.github.io/bootstrap
Yep removal of href="#" does the thing. But it seems ui-router scroll ui-sref=('#', 'element') only once. The second click did nothing. So to fix this thing i use something like that: this.scrollToElement = function (id) { $location.hash(id); $anchorScroll(); };
2

you can use this, so you can preserve the link and basically do nothing when its clicked

<a ui-sref="state" href="javascript:void(0);">Your link</a>

1 Comment

I have used your code like below which works <a ui-sref="state" href="#collapseOne">Your link</a>
1

I use this:

<a href-void>Click me! I don't do anything, but i'm still a link!</a>

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.