angularjs ng-click and href on anchor

后端 未结 2 1037
被撕碎了的回忆
被撕碎了的回忆 2021-01-18 21:47

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

2条回答
  •  一生所求
    2021-01-18 22:16

    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

提交回复
热议问题