How to make links with preventDefault in angular?

4

I'm using a function called with ng-click , through a link (Anchor). However, because of the hash # , the page is "jumping" every time I click that link.

My code is this:

<a ng-click="openImageModal(request)" href="#">
   Abrir modal
</a>

I would like to make a preventDefault in this link, so that it executes the function of angularjs , but without "jumping" the page, because of the hash # .

How to do this in AngularJS ?

I would like a solution without jQuery , because I want to get rid of dependency on it.

    
asked by anonymous 27.07.2016 / 17:07

3 answers

5
  • Remove% with%
  • add the following CSS class:

    a[ng-click]{
        cursor: pointer;
    }
    

This will eliminate default behavior while maintaining visual link feedback.

    
27.07.2016 / 17:09
4

Change the href="#" to href="javascript:void(0)" .

    
27.07.2016 / 17:12
4

If you set the href empty will also work:

<a href ng-click="addNinja('OnoSedai')">Adicionar Novo Ninja</a>
The advantage of doing so is that you will not need to define a CSS because of cursor:pointer absence, since links without href in some browsers do not come with cursor:pointer by default .

See working on Ideone:

link

    
27.07.2016 / 17:15