$scope's method called via ng-click: executed twice by IE
Asked Answered
W

2

7

In my controller:

$scope.homeAction = function() {
  console.log("HomeAction");
};

In my view:

<button ng-click="homeAction()">call homeAction()</button>

When clicking the button, the method gets executed as expected by Chrome and Firefox, but IE executes it twice. Any idea why?

Here is a plunker that reproduces the issue: http://plnkr.co/edit/pedZKjIVGDAYfMl0ZphJ.

Whitebeam answered 19/8, 2015 at 8:41 Comment(6)
@TributetoAPJKalamSir Tested with IE 11.Whitebeam
Executes only once on IE 11Pinnatipartite
@TributetoAPJKalamSir Weird! Just tried on browserstack.com, still reproducing...Whitebeam
github.com/driftyco/ionic/issues/2885Pinnatipartite
Yup even I also reproduces the same. Shocking.Pinnatipartite
add <form novalidate></form> and check whether it is working twice or onceArytenoid
W
13

Just add type="button"to your button and it should be fixed. Default behaviour is submit and apparently that messes with your code.

<ion-view title="Home">

  <ion-content padding="true">
    <button type="button" ng-click="homeAction()" class="button button-block button-positive">call homeAction()</button>
  </ion-content>

</ion-view>
Warfourd answered 19/8, 2015 at 9:26 Comment(0)
C
2

It seems to be related to the <button> event handling on Internet Explorer. Clicking it dispatches 2 events : MouseEvent and PointerEvent which explains why homeAction is called twice.

The easiest solution would be to change the <button> element to another DOM element (i.e. <a> or <span>)

Updated version using an <a> element http://plnkr.co/edit/Nn8CF7TnDKqsJA3unsp6

Another solution would be to verify which type of Event is dispatched and only allow MouseEvents. You can do this by passing the $event on your HomeAction and check the existence of the pointerType property (which is only available on TouchEvents). An example on plnkr : http://plnkr.co/edit/RmVHT1Pf2IeCNdmDH51T

$scope.homeAction = function($event) {
    if ($event.originalEvent.pointerType) {
      //PointerEvent, don't do anything
      return;
    }

    console.log("HomeAction");
};
Comminute answered 19/8, 2015 at 8:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.