Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent default anchor behaviour AngularJS

Tags:

When the user clicks a link, I want to execute some code in a function inside the controller. But I want to prevent that the URL changes.

I tried the following possibilities

  1. Removed the href-attribute. Didn't work, still changes the url to '/'

  2. Tried ng-click='deleteUser(user.id, $event)' and $event.preventDefault() in my deleteUser()-function. Didn't work.

  3. What did work is a hack I've found on GitHub about an unintended reload.

This is how I do it now:

<a ng-click="deleteUser(user.id)" href="javascript:">Delete user</a> 

Question

What is the'clean' method to prevent a link from changing the URL?

like image 973
ndequeker Avatar asked Sep 12 '12 08:09

ndequeker


People also ask

How would you add a hyperlink in a view in angular 4?

If you are using routing and want to add a href attribute to a specific route. Use the routerLink directive as it will add the href attribute to <a> tags.


1 Answers

<a ng-click="deleteUser(user.id)" href="">Delete user</a> 
like image 141
Umur Kontacı Avatar answered Sep 26 '22 08:09

Umur Kontacı