href overrides ng-click in Angular.js

231,059

Solution 1

You should probably just use a button tag if you don't need a uri.

Solution 2

This example from the angular documentation site just does href without even assigning it to an empty string:

[<a href ng-click="colors.splice($index, 1)">X</a>]

http://docs.angularjs.org/api/ng.directive:select

Solution 3

You can simply prevent the default behavior of the click event directly in your template.

<a href="#" ng-click="$event.preventDefault();logout()" />

Per the angular documentation,

Directives like ngClick and ngFocus expose a $event object within the scope of that expression.

Solution 4

Here is another solution :

<a href="" ng-click="logout()">Sign out</a>

i.e. Just remove the # from the href attribute

Solution 5

Just one more hint. If you need real URL (to support browser accessibility) you can do the following:

template:

<a ng-href="{{link}}" ng-click="$event.preventDefault(); linkClicked(link)">{{link}}</a>

directive:

$scope.linkClicked = function(link){
    // your code here
    $location.path(link);
};

In this way your code in linkClicked() will have chance to execute before navigating to the link

Share:
231,059
Paul
Author by

Paul

It is just a website. Life is somewhere else. Answers to downvoted question won't be accepted. Period. My wishlist The right syntax of with in Delphi: with r := obj.MyRecord do begin r.Field1 := 1; r.Field2 := '2'; // ... end; Favorites https://stackoverflow.blog/2019/10/17/imho-the-mythical-fullstack-engineer/ http://www.catb.org/esr/faqs/smart-questions.html Stackoverflow How to reduce image size on Stack Overflow Unicode Is there a list of characters that look similar to English letters? Windows How to programmatically get DLL dependencies Device misdetected as serial mouse Catch MSVCR120 missing error message in Delphi Get members of COM object via Delphi Olevariant type Controlling the master speaker volume in Windows 7 Filename timestamp in Windows CMD batch script getting truncated https://www.joachim-bauch.de/tutorials/loading-a-dll-from-memory Linux How to control backlight by terminal command MS Visual Studio How to change "Visual Studio 2017" folder location? Delphi Convert a null-delimited PWideChar to list of strings Out-of-the-box flat borderless button TFDConnection.OnRecover is never fired when PostgreSQL stops TScrollBox with customized flat border color and width? How to redirect mouse events to another control? Creating object instance based on unconstrained generic type how to create a TCustomControl that behaves like Tpanel? https://stackoverflow.com/a/43990453 - How to hack into a read-only direct getter property With DDevExtensions you can disable storing Explicit... properties in the dfm https://github.com/RomanYankovsky/DelphiAST C Arrow operator (-&gt;) usage in C The Definitive C++ Book Guide and List GDI+ How to rotate Text in GDI+? GDI+ performance tricks MSXML schema validation with msxml in delphi How can I get English error messages when loading XML using MSXML Inno Setup: Save XML document with indentation PostgreSQL Checking for existence of index in PostgreSQL Web Will the IE9 WebBrowser Control Support all of IE9's features, including SVG? http://howtocenterincss.com MVC for Web, MVP for Winforms and MVVM for WPF. Just an observation: Most of the claims such as "I can't understand what you wrote", "Please provide more details", etc. are made by people with a relatively low SO score. People with the highest score on SO understand everything. Template: This is an abandoned question. Author has no longer anything to do with the topic and can neither approve nor decline your answer.

Updated on July 08, 2022

Comments

  • Paul
    Paul almost 2 years

    When both, href and ng-click attributes are defined:

    <a href="#" ng-click="logout()">Sign out</a>
    

    the href attribute takes precedence over ng-click.

    I am looking for a way to raise priority of ng-click.

    href is required for Twitter Bootstrap, I can't remove it.

  • Geoff
    Geoff about 11 years
    Yes, if you can elaborate on how Twitter Bootstrap requires it, maybe I can help more.
  • Sindre
    Sindre almost 11 years
    For the question asked, this should be the solution. Worked on Angular 1.1.5 as well
  • duckegg
    duckegg over 10 years
    It seems <a href="" ng-click=""> will prevent the ng-click in Android 2.3.x browser. I finally use <a href="javascript:void(0)" ng-click="">
  • Jiří Vypědřík
    Jiří Vypědřík over 10 years
    Duckegg's suggestion is the best one
  • Rhys van der Waerden
    Rhys van der Waerden almost 10 years
    When I try this all of the links appear to be visited, which is not the behaviour I'm after. The other way (href="#") causes a page reload, which is also wrong.
  • Darrrrrren
    Darrrrrren over 9 years
    How does this work with search engines? I am using AngularJS routing and need to maintain state in a service so for all of my internal application links I use $location, but removing href make it impossible for search engines to follow along the site.
  • Juampy NR
    Juampy NR about 9 years
    This makes IE9 not to show a hand cursor. Using href="" solves it.
  • thenetimp
    thenetimp almost 9 years
    @juampy, the hand over the link can be handled with CSS. This answer given is the official AngularJS way to do it.
  • Tom Grant
    Tom Grant over 8 years
    This is a great solution. If you have an href, you can right click to open it in a new tab, but on left click it calls ng-click. Exactly what I was looking for
  • Jim109
    Jim109 over 8 years
    Works, the link doesn't change your url and isn't marked as visited. Best answer!
  • Exocom
    Exocom over 8 years
    This solution works and only changes the left click behaviour, right click stays intact (context menu), including the possibility to a link in href. So that is a more general solution compared to TooMuchTenacious, which actually answers the question more directly.
  • sheriffderek
    sheriffderek about 8 years
    The # will get treated like hash link.
  • sheriffderek
    sheriffderek about 8 years
    buttons can't have other element inside them, so for styling, you wouldn't want to do that - if you need things inside of it.
  • Jay Jay Jay
    Jay Jay Jay about 8 years
    Once again an excellent answer. Both left and right click work
  • Jason Maggard
    Jason Maggard about 8 years
    Worked great for allowing Bootstrap carousel controls without triggering routing. Thanks!
  • Marios Fakiolas
    Marios Fakiolas almost 8 years
    <a href="javscript:void(0)" ng-click="logout()">Sign out</a> will serve you well too
  • Guilherme Iazzetta
    Guilherme Iazzetta over 6 years
    Perfect! I can continue with links and get more index in Google and use ng-click to call in Ajax. Thanks.
  • Josue Rocha
    Josue Rocha over 6 years
    Great solution, quick and useful. Thanks!
  • Naila Akbar
    Naila Akbar over 4 years
    You saved me.. Thanks a lot.