Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angularjs ng-click and href on anchor

If there's an ng-click and a href together on an anchor the ng-click doesn't work. I want both to work if possible, go to a route and then run a function. My use-case is I have a drawer open with a panel of links, when going to a link I want to close the drawer.

What's an easy way to get both working at the same time?

like image 946
Harry Avatar asked Jul 18 '13 07:07

Harry


People also ask

How do I bind a href in AngularJS?

The ng-href directive overrides the original href attribute of an <a> element. The ng-href directive should be used instead of href if you have AngularJS code inside the href value. The ng-href directive makes sure the link is not broken even if the user clicks the link before AngularJS has evaluated the code.

What is ngInit?

The ngInit directive allows you to evaluate an expression in the current scope. This directive can be abused to add unnecessary amounts of logic into your templates. There are only a few appropriate uses of ngInit : aliasing special properties of ngRepeat , as seen in the demo below.


1 Answers

Once you navigate away from the page scripts no longer run.*

Your flow can be something like:

When the user clicks, run a function that does the following:

  • Run the original function you wanted to run
  • Handle any 'navigate away in this specific case' logic.
  • Change location.href to the URL you want to to navigate to.

It's of course possible to extract this behavior to a directive if you find yourself implementing that bit of logic over and over :)

* You can use onbeforeunload but that's not good for plenty of other reasons

like image 83
Benjamin Gruenbaum Avatar answered Sep 27 '22 20:09

Benjamin Gruenbaum