AngularJS ng-click fires twice

后端 未结 18 1738
死守一世寂寞
死守一世寂寞 2020-12-03 07:10

I am using ng-click and it fires twice when I apply it to SPAN tag.

HTML

相关标签:
18条回答
  • 2020-12-03 07:55

    Same problem using

    <a ng-click="fn()"></a>
    

    fn was called twice

    Using a button fixed it :

    <button ng-click="fn()"></button>
    
    0 讨论(0)
  • 2020-12-03 07:55

    I solved this by removing my ngsubmit handler as I don't have a need for it. I'm monitoring on change events and using SignalR to update the screen in near real-time.

    I was also in a form and the AngularJS docs for ngSubmit states:

    Warning: Be careful not to cause "double-submission" by using both the ngClick and ngSubmit handlers together. See the form directive documentation for a detailed discussion of when ngSubmit may be triggered.

    0 讨论(0)
  • 2020-12-03 07:58

    I got it when I accidently called $compile for the dynamically added elements several times in the cycle, instead of just once. Compiling just once removed this effect.

    0 讨论(0)
  • 2020-12-03 07:59

    This is probably obscure, but I had ng-click firing twice because I had BrowserSync running, which would mirror my inputs into a tab that I had open in another window, thus doubling up all my clicks. To resolve, I disabled “ghostMode”: https://www.browsersync.io/docs/options/

    0 讨论(0)
  • I had a problem like this as well.

    <button style="width: 100%;" class="btn btn-danger" ng-click="'{{vm.delete()}}'">
    

    Thats not how you call ng-click, but no errors were thrown and the function calls still worked.

    <button style="width: 100%;" class="btn btn-danger" ng-click="vm.delete()">
    

    Is correct and will then only be called once.

    0 讨论(0)
  • 2020-12-03 08:00

    If other answers don't help, make sure that AngularJS profiling is disabled in Batarang (if you have it installed of course).

    This was making ng-click to fire twice for me.

    enter image description here

    0 讨论(0)
提交回复
热议问题